Question

I have an HttpPost which sends data to a server to be stored on a database. When that data is successfully stored I get a response in my LogCat that says "message has been saved successfully" (this response was defined in my PHP code). I am happy with that, but I am trying to get that same response to be displayed in a Toast. Here is my code:

String myBreadfromr, myBreadtor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Bundle myBasket = getIntent().getExtras();
    myBreadfromr = myBasket.getString("keyfromcellr");
    myBreadtor = myBasket.getString("keytocellr");
    new SendData().execute("");
}

public class SendData extends AsyncTask<String, Integer, Void> {

    protected void onPreExecute(String f) {
        // called before doInBackground has started
        f = "f";
    }

    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub
        // Create a new HTTP client
        HttpClient client = new DefaultHttpClient();
        // Create a new HTTP Post
        HttpPost post = new HttpPost("http://192.xxx.xxx.xxx/androidp2p/process.php");
        try {
            // Add the data
            List<NameValuePair> pairs = new ArrayList<NameValuePair>(3);
            pairs.add(new BasicNameValuePair("from", myBreadfromr));
            pairs.add(new BasicNameValuePair("to", myBreadtor));
            pairs.add(new BasicNameValuePair("message", "What is your location?"));
            // Encode Post data into valid URL format
            post.setEntity(new UrlEncodedFormEntity(pairs));
            // Go back to the first page
            Intent back2start = new Intent(RequestLocation.this, StartApp.class);
            startActivity(back2start);
            // Make the HTTP Post Request
            HttpResponse response = client.execute(post);
            // Convert the response into a String
            final HttpEntity resEntity = response.getEntity();
            // Write the response to a log file
            if (resEntity != null) {
                Log.i("RESPONSE", EntityUtils.toString(resEntity));
            }
            runOnUiThread(new Runnable(){
                   public void run() {
                        Toast.makeText(RequestLocation.this, resEntity.toString(), Toast.LENGTH_LONG).show();
                   }
            });
        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        } catch (ClientProtocolException cpe) {
            cpe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        // called when the background task has made any progress
    }

    protected void onPostExecute() {
        // called after doInBackground has finished
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
}

What I see in the Toast instead is: "org.apache.http.conn.BasicManagedEntity@41284b48".

Thanking you in advance for any help in resolving this matter.

Was it helpful?

Solution

Use EntityUtils.toString(resEntity) in the Toast to get the same text.

Also no need to call runOnUiThread, doInBackground must return something, not null, and that something will be available onPostExecute which already is made to run on the UI thread.

AsyncTask

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top