GetHttpResponseCode () returns -1 in android 2.2

In my Android application, I am trying to retrieve data from the server by doing a POST request.

I use the HttpURLConnection class to make requests since Apache HttpClient no longer supported by android.

That's what I'm doing.

 private boolean callWS() { try { // To avoid the bug in httpurlconnection prior froyo which // causes the getInputStream to return headers along with response if (Build.VERSION.SDK_INT < 8) System.setProperty("http.keepAlive", "false"); mHttpResponseCode = 0; mErrorMessage = ""; // Initialize connection URL connectURL = new URL(mServerUrl); HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); conn.setReadTimeout(30000); conn.setConnectTimeout(15000); conn.setRequestMethod("POST"); // Set some headers connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Accept-Encoding", "deflate, gzip"); connection.setRequestProperty("Content-Length", mParameters.length() + ""); // Connect to host conn.connect(); // Write parameters to connection OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(mParameters); writer.flush(); writer.close(); // Wait for http response code mHttpResponseCode = conn.getResponseCode(); // Read response from connection BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); ByteArrayBuffer baf = new ByteArrayBuffer(50); int read = 0; int bufSize = 1024; byte[] buffer = new byte[bufSize]; while (true) { read = bis.read(buffer); if (read == -1) break; baf.append(buffer, 0, read); } // Decompress gzipped response if (conn.getHeaderField("Content-Encoding") != null && conn.getHeaderField("Content-Encoding").contains("gzip")) mResponseString = decompress(baf.toByteArray()); else mResponseString = new String(baf.toByteArray()); mResponse.setResponse(mResponseString); isWSCallSuccessfull = true; } catch(UnknownHostException unknownHostException) { isWSCallSuccessfull = false; mErrorMessage = "Unknown host exception"; unknownHostException.printStackTrace(); mLogger.putStacktrace(unknownHostException); } catch(SocketException socketException) { isWSCallSuccessfull = false; mErrorMessage = "Socket Exception"; socketException.printStackTrace(); mLogger.putStacktrace(socketException); } catch(SocketTimeoutException socketTimeOutException) { isWSCallSuccessfull = false; mErrorMessage = "Socket Timeout Exception"; socketTimeOutException.printStackTrace(); mLogger.putStacktrace(socketTimeOutException); } catch(SSLException sslException) { isWSCallSuccessfull = false; mErrorMessage = "SSL Exception"; sslException.printStackTrace(); mLogger.putStacktrace(sslException); } catch(IOException ioException) { isWSCallSuccessfull = false; mErrorMessage = "IO Exception " + ioException.getMessage(); ioException.printStackTrace(); mLogger.putStacktrace(ioException); } mResponse.setHttpResponseCode(mHttpResponseCode); mResponse.setErrorMessage(mErrorMessage); mResponse.isWSCallSuccessful(isWSCallSuccessfull); return isWSCallSuccessfull; } 

This works fine on all devices except devices running 2.2 (not tried in 2.1).

In 2.2 it works fine. But if I leave this part of the code unoccupied for more than 30 seconds, it returns me with -1 as the HTTP response code next time.

One more note: this only happens with HTTPS URLs, not HTTP addresses. I do not want to use the HttpsURLConnection class, because sometimes I can also use http.

I do not close the connection to maintain the connection. What am I doing wrong?

+7
source share
1 answer

If you want to use both Https and Http at the same time and do not want to create a separate connection, and if HttpsUrlConnection solves your "-1" problem, you can use the following approach:

 URLConnection conn = new URL(url).openConnection(); if (conn instanceof HttpsURLConnection) { // do stuff with cast to HttpsUrlConection } else { // do stuff with cast to HttpUrlConnection } 

I accepted this answer as a link

0
source

All Articles