1

I am new to Java and I am trying to figure out how to receive a response from an HTTP request to a PHP page.

here is my main HTTP 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);
        //Do anything with response..
    }
}

and this is what I am using to send the request:

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

which is nestled in an on-click. It works flawlessly, it adds the user to the table using the php file. Now how am I supposed to let my activity know that the entry was successful? Is there a way to read a response?

I tried searching but maybe I am looking for the wrong answers here, just a little confused. Thank you for any help or direction.

user3053446
  • 124
  • 2
  • 13

1 Answers1

0

You can use interface as a callback to the activity.

Define a interface. Let your activity implement the interface. Use the interface as a cll back to the activity and return responseString.

Example :

How do I return a boolean from AsyncTask?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256