如何从HttpResponse打印返回的消息?
我的Android手机上有此代码。
URI uri = new URI(url); HttpPost post = new HttpPost(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
我有一个在页面中加载此的asp.net Webform应用程序
Response.Output.Write("It worked");
我想从HttpReponse获取此Response并打印出来。我该怎么做呢?
我试过了,response.getEntity().toString()
但似乎只是打印出内存中的地址。
谢谢
回答:
使用ResponseHandler
。一行代码。有关使用它的示例Android项目,请参见此处和此处。
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
}
}
在http://www.androidsnippets.org/snippets/36/上添加此帖子的组合并完成HttpClient
以上是 如何从HttpResponse打印返回的消息? 的全部内容, 来源链接: utcz.com/qa/431171.html