Android HttpPost:如何获取结果 [英] Android HttpPost: how to get the result

查看:28
本文介绍了Android HttpPost:如何获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试发送 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:

Try to use the EntityUtil on your response:

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

这篇关于Android HttpPost:如何获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆