I have this code on my Android phone.
我的Android手机上有这个代码。
URI uri = new URI(url);
HttpPost post = new HttpPost(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
I have a asp.net webform application that has in the page load this
我有一个asp.net webform应用程序,在页面加载这个
Response.Output.Write("It worked");
I want to grab this Response from the HttpReponse and print it out. How do I do this?
我想从HttpReponse中获取此响应并将其打印出来。我该怎么做呢?
I tried response.getEntity().toString()
but it just seems to print out the address in memory.
我尝试了response.getEntity()。toString()但它似乎打印出内存中的地址。
Thanks
谢谢
4 个解决方案
#1
38
Use ResponseHandler
. One line of code. See here and here for sample Android projects using it.
使用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
}
}
add combination of this post and complete HttpClient at - http://www.androidsnippets.org/snippets/36/
添加此帖子的组合并完成HttpClient - http://www.androidsnippets.org/snippets/36/
#2
8
I would just do it the old way. It's a more bulletproof than ResponseHandler, in case you get different content types in the response.
我会以旧的方式做到这一点。如果您在响应中获得不同的内容类型,它比ResponseHandler更具防弹性。
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
byte [] responseBody = outstream.toByteArray();
#3
6
I used the following code
我使用了以下代码
BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder total = new StringBuilder();
String line = null;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
return total.toString();
#4
3
This code will return the entire response message in respond as a String
, and status code in rsp, as an int
.
此代码将返回整个响应消息作为字符串进行响应,并将状态代码作为int返回到rsp中。
respond = response.getStatusLine().getReasonPhrase();
rsp = response.getStatusLine().getStatusCode();`
#1
38
Use ResponseHandler
. One line of code. See here and here for sample Android projects using it.
使用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
}
}
add combination of this post and complete HttpClient at - http://www.androidsnippets.org/snippets/36/
添加此帖子的组合并完成HttpClient - http://www.androidsnippets.org/snippets/36/
#2
8
I would just do it the old way. It's a more bulletproof than ResponseHandler, in case you get different content types in the response.
我会以旧的方式做到这一点。如果您在响应中获得不同的内容类型,它比ResponseHandler更具防弹性。
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
response.getEntity().writeTo(outstream);
byte [] responseBody = outstream.toByteArray();
#3
6
I used the following code
我使用了以下代码
BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder total = new StringBuilder();
String line = null;
while ((line = r.readLine()) != null) {
total.append(line);
}
r.close();
return total.toString();
#4
3
This code will return the entire response message in respond as a String
, and status code in rsp, as an int
.
此代码将返回整个响应消息作为字符串进行响应,并将状态代码作为int返回到rsp中。
respond = response.getStatusLine().getReasonPhrase();
rsp = response.getStatusLine().getStatusCode();`