-2

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.

Keegs
  • 95
  • 3
  • 11
  • 3
    Should you be using ``EntityUtils.toString(resEntity)`` for creating Toast String too instead of ``resEntity.toString``? – harism Mar 24 '13 at 07:23
  • As @harism says. But couldn't you work that out for yourself? – Squonk Mar 24 '13 at 07:28
  • Thanks. In addition, you must comment out the `Log.i("RESPONSE", EntityUtils.toString(resEntity))` for the code to work, because `EntityUtils` cannot be called in the same code twice I believe. And like @gpasci said for good programming practice, run the `Toast` in the `onPostExecute` by making this:`String data = EntityUtils.toString(resEntity);` in the `doInBackground`, and then using that string "data" in the `Toast`. – Keegs Mar 27 '13 at 14:21

1 Answers1

0

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

gpasci
  • 1,420
  • 11
  • 16
  • @harism - When I use EntityUtils.toString(resEntity) in the Toast instead of resEntity.toString(), it asks me to surround with a try and catch, which compiles. However, when I run the code on the emulator, when the Toast is called the emulator crashes. – Keegs Mar 24 '13 at 16:10
  • If I put the Toast in onPostExecute, an error pops up that says "resEntity cannot be resolved". And also, does doInBackground have to return something if I use AsyncTask instead of AsyncTask ? - @gpasci – Keegs Mar 24 '13 at 16:43