HttpUrlConnection behaves differently in Android 2.3 Gingerbread

I'm here. I use the same HttpUrlConnection code to get JSON from my server with Android 1.5. Suddenly, from 2.3, this will not work, and I do not understand why.

Here is my code:

try { HttpURLConnection conn = (HttpURLConnection)(new URL(Constants.SERVER_URL + path)).openConnection(); conn.setAllowUserInteraction(false); // no user interact [like pop up] conn.setDoOutput(true); // want to send conn.setDoInput(true); conn.setRequestMethod( "POST" ); conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); conn.setRequestProperty( "Accept", "application/json,text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" ); conn.setRequestProperty( "Accept-Charset", "UTF-8" ); conn.setRequestProperty("Connection", "Keep-Alive"); DataOutputStream pw = new DataOutputStream (conn.getOutputStream()); pw.writeBytes(requestString); pw.flush(); pw.close(); if (conn.getResponseCode() == 200) { InputStream in = new BufferedInputStream(conn.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringBuffer stringBuffer = new StringBuffer(); while(br.ready()){ stringBuffer.append(br.readLine()); } br.close(); in.close(); return stringBuffer.toString(); } else { return badResponse; } } catch (Exception ioe) { return ERROR_MAKING_CONNECTION; } 

The response is returned with code 200, however, when I run conn.getInputStream (). available (), the result is -1. On all other android aviins, besides 2.3, conn.getInputStream (). Available () returns a significant response.

Any ideas?

+4
source share

Source: https://habr.com/ru/post/1335458/


All Articles