Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Android not send my second SSL request?
    primarykey
    data
    text
    <p>My Android app is talking to a webserver using SSL and GET requests. After switching to API 10 (Gingerbread) the SSL connection works - but only for the first time after the app starts...</p> <p>The first request is sent by the main activity - after getting a response, another activity starts and sends multiple requests. And none of them is answered. In both cases the request is sent using a litte WebService class that is initiated in a new AsyncTask. After downsizing this alass, the only thing it actually contains is the URL(-String). Each activity starts its own instance of this class.</p> <p>Here is the method that should do the GET request. As easily visible I included some code to avoid keep-alive - not that I don't like it, but it has been suggested in other answers to do so to avoid problems with multiple connections. Well, it did not work in my case.</p> <pre><code>public String webGet(String methodName, Map&lt;String, String&gt; params) { String getUrl = webServiceUrl + methodName; index++; final int connectionID = index; int i = 0; for (Map.Entry&lt;String, String&gt; param : params.entrySet()) { if (i == 0) { getUrl += "?"; } else { getUrl += "&amp;"; } try { getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } i++; } String response; Log.e("WebGetURL", "["+connectionID+"] " + getUrl); URL url; try { url = new URL(getUrl); } catch (MalformedURLException e) { Log.e("WebService", "Malformed URL: " + getUrl); return null; } HttpURLConnection urlConnection; try { Log.e("WebGetResponse", "["+connectionID+"] openConnection()"); System.setProperty("http.keepAlive", "false"); if (webServiceSsl) { Log.e("WebService", "Using HTTPS"); urlConnection = (HttpsURLConnection) url.openConnection(); } else { urlConnection = (HttpURLConnection) url.openConnection(); } urlConnection.setUseCaches(false); urlConnection.setRequestProperty("Connection","Keep-Alive"); urlConnection.setDoOutput(false); urlConnection.setDoInput(true); urlConnection.setRequestMethod("GET"); } catch (IOException e) { Log.e("WebService", "I/O exception opening connection: " + e.getMessage()); e.printStackTrace(); return null; } urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(10000); urlConnection.setRequestProperty("Connection", "close"); try { urlConnection.connect(); Log.e("WebGetResponse", "["+connectionID+"] getInputStream()"); // This is the last thing I hear from my thread BufferedInputStream bin = new BufferedInputStream(urlConnection.getInputStream()); Log.e("WebGetResponse", "["+connectionID+"] gotInputStream()"); byte[] contents = new byte[1024]; int bytesRead=0; StringBuilder strFileContents = new StringBuilder(); Log.e("WebGetResponse", "["+connectionID+"] Waiting for data"); while((bytesRead = bin.read(contents)) != -1) { String add = new String(contents, 0, bytesRead); strFileContents.append(add); } bin.close(); response = strFileContents.toString(); } catch (IOException e) { Log.e("WebService", "I/O exception reading stream: " + e.getMessage()); e.printStackTrace(); return null; } finally { urlConnection.disconnect(); } Log.e("WebGetResponse", "["+connectionID+"] " + response); return response; } </code></pre> <p>I have been trying ans searching - I don't get the problem. Actually I cannot test the class on a non-https server currently, so I am unaware if the problem occurs in HTTP as well. However, the handshake seems to work, because the first request works well.</p> <p>And here is the code that should start the request (final param is the GET content to send):</p> <pre><code> class ServerDataThread extends AsyncTask&lt;Integer, Integer, String[]&gt; { @Override protected String[] doInBackground(Integer... attempts) { sendActive++; int count = attempts.length; String[] responses = new String[count]; for (int i = 0; i &lt; count; i++) { responses[i] = server.webGet("collector.php", params); } return responses; } protected void onPostExecute(String[] responses) { sendActive--; for (int i = 0; i &lt; responses.length; i++) { if (responses[i] == null) { continue; } onResponseData(responses[i]); } } } new ServerDataThread().execute(0); </code></pre> <p>Could anyone please help me out with a hint what I am doing wrong? Thank you very much!</p> <p>BurninLeo</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload