Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read HTTP response using AsyncTask?
    text
    copied!<p>I am having difficulty trying to get the body of the HTTP response from the string. So according to my code, I should have response from the PHP page in the string called "responseString" however because this is in an ASynchTask, I can not access that string anywhere else, so how can I receive the string?</p> <p>For example, I would like to shoot this string into a text view for testing purposes, how can I do that? Am I reading the code incorrectly? Is the response I am getting not being put into that string?</p> <p>Here is my code:</p> <pre><code>class RequestTask extends AsyncTask&lt;String, String, String&gt;{ @Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else{ //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { //TODO Handle problems.. } catch (IOException e) { //TODO Handle problems.. } return responseString; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); } } </code></pre> <p>I am using the following to execute:</p> <pre><code>new RequestTask().execute("http://www.mywebsite.com/android/registercheck.php?first=" + first2 + "&amp;last=" + last2 + "&amp;dispname=" + display2 + "&amp;email=" + email2 + "&amp;password=" + password2 ); </code></pre>
 

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