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

查看:83
本文介绍了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 对您的回复:

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

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

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