1

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 );
2
  • you can use Toast or anything that worked with UI in onPostExecute. Commented Dec 29, 2013 at 11:56
  • I wish I could, but everytime I implement code in that section, I get an error. for example: t=(TextView)findViewById(R.id.resulttext); t.setText(responseString); returns: The method findViewById(int) is undefined for the type RequestTask Commented Dec 29, 2013 at 12:17

2 Answers 2

1

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

}
12
  • I tried this already, but I get "responseString cannot be resolved to a variable" Why is that? Commented Dec 29, 2013 at 11:58
  • usually this error occurs when the variable isnt declared well. add Log.d("check response", responseString); after responseString = out.toString(); to check the value of the String
    – Coderji
    Commented Dec 29, 2013 at 12:02
  • and tell me the result
    – Coderji
    Commented Dec 29, 2013 at 12:05
  • @user3053446 you must move String responseString = null; above doInBackground you define that in doInBackground, you cant use that into any other place Commented Dec 29, 2013 at 12:10
  • The result is correct, it shows the entirety of my page. Now the problem is getting that to a global variable.... How can I do this? Commented Dec 29, 2013 at 12:11
0

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();
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.