我的 Android 手机上有此代码。
URI uri = new URI(url);
HttpPost post = new HttpPost(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
我有一个 ASP.NET Web 表单应用程序,在页面中加载此内容
Response.Output.Write("It worked");
我想从 HttpResponse 中获取此 Response 并将其打印出来。我该怎么做呢?
I tried response.getEntity().toString()
但它似乎只是打印出内存中的地址。
Thanks
Use ResponseHandler http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientWithResponseHandler.java。一行代码。看here https://github.com/commonsguy/cw-android/tree/master/Service/WeatherAPI and here http://github.com/commonsguy/cw-andtutorials/tree/master/15-HttpClient/Patchy/使用它的示例 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
}
}
添加这篇文章和完整的 HttpClient 的组合 -http://www.androidsnippets.org/snippets/36/ http://www.androidsnippets.org/snippets/36/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)