如何从Htt的presponse打印出返回的消息? [英] How to print out returned message from HttpResponse?
问题描述
我有我的Android手机这个code。
I have this code on my Android phone.
URI uri = new URI(url);
HttpPost post = new HttpPost(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
我有一个在页面加载一个asp.net web窗体应用此
I have a asp.net webform application that has in the page load this
Response.Output.Write("It worked");
我要抓住这个响应来自型Htt preponse并打印出来。如何做到这一点?
I want to grab this Response from the HttpReponse and print it out. How do I do this?
我试过 response.getEntity()的toString()
,但它只是似乎打印出内存中的地址。
I tried response.getEntity().toString()
but it just seems to print out the address in memory.
感谢
推荐答案
使用<一个href="http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java"><$c$c>ResponseHandler$c$c>.一行code。请参见 href="http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/">和的此处使用它的样品的Android项目。
Use ResponseHandler
. One line of code. See here and here for sample Android projects using it.
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/user");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
JSONObject response=new JSONObject(responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
补充相结合的这篇文章,完成的HttpClient在 - <一个href="http://www.androidsnippets.org/snippets/36/">http://www.androidsnippets.org/snippets/36/
这篇关于如何从Htt的presponse打印出返回的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!