gpt4 book ai didi

java - 如何打印从 HttpResponse 返回的消息?

转载 作者:IT老高 更新时间:2023-10-28 20:25:36 26 4
gpt4 key购买 nike

我的 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.getEntity().toString() 但它似乎只是打印出内存中的地址。

谢谢

最佳答案

使用 ResponseHandler .一行代码。见 herehere使用它的示例 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 的组合

关于java - 如何打印从 HttpResponse 返回的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2573064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com