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 );
Jonik
  • 80,077
  • 70
  • 264
  • 372
user3053446
  • 124
  • 2
  • 13
  • you can use Toast or anything that worked with UI in `onPostExecute`. – Shayan Pourvatan Dec 29 '13 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 – user3053446 Dec 29 '13 at 12:17

2 Answers2

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

}
Coderji
  • 7,655
  • 5
  • 37
  • 51
  • I tried this already, but I get "responseString cannot be resolved to a variable" Why is that? – user3053446 Dec 29 '13 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 Dec 29 '13 at 12:02
  • and tell me the result – Coderji Dec 29 '13 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 – Shayan Pourvatan Dec 29 '13 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? – user3053446 Dec 29 '13 at 12:11
  • I tried to do this but every single time I try to implement any code in the `onPostExecute`, I get a "The Method * is undefined for the type RequestTask" For example, I am trying to run this: `t=(TextView)findViewById(R.id.resulttext);t.setText(responseString);` – user3053446 Dec 29 '13 at 12:15
  • read what i read, the return value of `doInBackground` is the `result` argument in `onPostExecute` so basically it should looks like this `t=(TextView)findViewById(R.id.resulttext); t.setText(result);` you should use `result` variable not `responseString` – Coderji Dec 29 '13 at 12:17
  • Yes, I tried this, same result, I get `The method findViewById(int) is undefined for the type RequestTask` error – user3053446 Dec 29 '13 at 12:19
  • ok to put the variable as global add `String responseString` between `doInBackground` and `class RequestTask`. by this you have initialize it as global throughout this class. but I dont think this will solve your problem. inform me what will happen – Coderji Dec 29 '13 at 12:21
  • I can't even toast in this section, I get this error: `The method getApplicationContext() is undefined for the type RequestTask` – user3053446 Dec 29 '13 at 12:27
  • is the AsyncTask class is standalone class, or is it inner class with other activity?! – Coderji Dec 29 '13 at 12:36
  • If you want to show a Toast from RequestTask, you simply need to pass the Context (e.g. Activity) to it. Add a Context field & construcutor parameter in RequestTask. – Jonik Dec 29 '13 at 14:39
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();
 }
}
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63