user3053446
user3053446

Reputation: 124

How to read HTTP response using AsyncTask?

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?

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?

Here is my code:

class RequestTask extends AsyncTask<String, String, String>{

    @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);



    }
}

I am using the following to execute:

new RequestTask().execute("http://www.mywebsite.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&dispname=" + display2 + "&email=" + email2 + "&password=" + password2 );

Upvotes: 1

Views: 2833

Answers (2)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

I think your problem don't solved yet.so,

for Log or Toast use following code:

class RequestTask extends AsyncTask<String, String, String>{
String responseString = "";
@Override
protected String doInBackground(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;

    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);
    Log.d("Result is", responseString );
    Toast.makeText(YourClass.this , responseString , Toast.LENGTH_SHORT).show();
 }
}

Upvotes: 0

Coderji
Coderji

Reputation: 7745

after executing the doInBackground the onPostExecute will launch directly, and there you can update the UI so since you are returning responseString, then in onPostExecute put it in a TextView

The return of doInBackgound is the arugment of onPostExecute so the String result is your responseString.

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    tv.setText(result);

}

Upvotes: 1

Related Questions