Android的HttpPost:如何获得结果如何获得、结果、Android、HttpPost

2023-09-12 00:25:25 作者:百无一用是深情

我一直在试图长期发送HttpPost请求并检索响应,但即使我能够做出一个方面,我还没有得到如何获取这是由请求 - 响应返回的字符串消息

I have been trying long to send an HttpPost request and retrieve response but even though I was able to make a connection I don't yet get how to get the string message which is returned by the request-response

 HttpClient httpclient = new DefaultHttpClient();
 HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php");
 // Add your data   
 List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (5);
 nameValuePairs.add(new BasicNameValuePair("type", "20"));
 nameValuePairs.add(new BasicNameValuePair("mob", "919895865899"));
 nameValuePairs.add(new BasicNameValuePair("pack", "0"));
 nameValuePairs.add(new BasicNameValuePair("exchk", "1"));

 try {
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     Log.d("myapp", "works till here. 2");
     try {
         HttpResponse response = httpclient.execute(httppost);
         Log.d("myapp", "response " + response.getEntity());
     } catch (ClientProtocolException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
 } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
 } 

我很抱歉,我听起来很幼稚,因为我是新来的Java。请帮我。

I'm sorry, I sound very naive because I'm new to java. Please help me.

推荐答案

尝试使用 EntityUtil 对您的回复:

String responseBody = EntityUtils.toString(response.getEntity());