1

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.

Ricardo
  • 9,136
  • 3
  • 29
  • 35
  • Try see: [Topic in Stakeoverflow](http://stackoverflow.com/questions/11810447/httpurlconnection-worked-fine-in-android-2-x-but-not-in-4-1-no-authentication-c, "Stakeoverflow") - my advice is: use [Volley](https://developer.android.com/training/volley/request.html, "Android Developer") – Peter R Mar 20 '17 at 11:13
  • i tried your snipped and it worked. I `POST`ed `hello world` to `http://stackoverflow.com/` – nandsito Mar 20 '17 at 12:24
  • and `UnknownHostException: No address associated with hostname` happens, among other reasons, when there is no internet connection – nandsito Mar 20 '17 at 12:26
  • Check my post, I clearly said I have two emulators next to each other and it works with API 25 and not API 18 (4.3) – Ricardo Mar 20 '17 at 12:29
  • It get's stuck at conn.getOutPutStream(); – Ricardo Mar 20 '17 at 12:29
  • Dont use an emulator. Take a real device. – greenapps Mar 20 '17 at 13:05
  • `The error thrown is UnknownHostException:`. Then show the url you use. What kind of host? – greenapps Mar 20 '17 at 13:06
  • `writer.close(); os.close();`. Do not close the streams as it will close the connection too. – greenapps Mar 20 '17 at 13:08
  • `conn.connect();`. It makes no sense trying to connect if you already have a connection and already send your data. So remove. – greenapps Mar 20 '17 at 13:09
  • `i tried your snipped and it worked. I POSTed hello world to http://stackoverflow.com/ `. Interesting. What data did you get as response? @nandsito – greenapps Mar 20 '17 at 13:12
  • i got stackoverflow home HTML – nandsito Mar 20 '17 at 13:14
  • @greenapps it is a link which you can only access from my working web connection, so no point posting it here. I know the code works, at least for all the Android versions except jelly bean. Have you tried running that code from a jelly bean phone/emulator? It seems it doesn't work with any of them. I don't have a single record from request with jelly bean phones – Ricardo Mar 20 '17 at 14:21
  • ???? Have you removed the code lines which i asked you to remove? Why dont you react? – greenapps Mar 20 '17 at 14:24
  • I got that bit of code from here: http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post which always worked. Seems to be pretty much the same everywhere. I did remove the lines you said and got the same result. In debug mode, the code never goes past `conn.getOutputStream()` – Ricardo Mar 20 '17 at 14:30
  • in the JellyBean emulator, open a web-browser (ex, the one that comes with the OS), and try to access your link, what is the result ? – Yazan Mar 20 '17 at 14:36
  • Interesting. My emulator running Jelly bean cannot reach the link through the browser – Ricardo Mar 20 '17 at 14:43
  • ahaa, so it's not about the code :), it's network issue, NAT and so on, now you know where to look ;) – Yazan Mar 20 '17 at 14:58
  • I was able to replicate my error and it had to do with the SSLv3 protocol from versions below 4.4. Thank you everyone for trying to help me out – Ricardo Mar 21 '17 at 13:25

0 Answers0