HttpUrlConnection doesn't work in Jelly Bean

109 Views Asked by At

I'm having an issue concerning HttpUrlConnection. It works perfectly in every OS besides JellyBean and I have no idea why. I have 2 emulators next to each other, one running Nougat, another running JellyBean API 18, in the Nougat the request goes through and in the JellyBean it doesn't

Here is my code:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(60000);
conn.setConnectTimeout(1000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
OutputStream os = conn.getOutputStream(); <-- Gets stuck here and times out after the 60 seconds I gave in the readtimeout
BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(paramList));
writer.flush();
writer.close();
os.close();

conn.connect();

BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result2 = bis.read();
while(result2 != -1) {
    buf.write((byte) result2);
    result2 = bis.read();
}
result = buf.toString();

The error thrown is UnknownHostException: No address associated with hostname but I figure it is not relevant as it comes from the time out expiration.

Any help is appreciated.

0

There are 0 best solutions below