如何让Toast显示LogCat文本?

时间:2013-03-24 07:22:07

标签: android logcat toast server-response

我有一个HttpPost,它将数据发送到服务器以存储在数据库中。当成功存储该数据后,我在LogCat中得到一条消息“消息已成功保存”(此响应在我的PHP代码中定义)。我对此感到高兴,但我想尝试在Toast中显示相同的响应。这是我的代码:

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
}

我在Toast中看到的是:“org.apache.http.conn.BasicManagedEntity@41284b48”。

提前感谢您提供解决此事的任何帮助。

1 个答案:

答案 0 :(得分:0)

在Toast中使用EntityUtils.toString(resEntity)来获取相同的文字。

也无需致电runOnUiThreaddoInBackground必须返回一些内容,而不是null,以及可以在UI上运行的某些内容onPostExecute线程。

AsyncTask

相关问题