我一直在尝试发送HttpPost请求并获取响应,但是即使我能够建立连接,我仍然无法获得如何获取由请求-响应返回的字符串消息。
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新手。请帮我。
尝试EntityUtil在您的响应中使用:
EntityUtil
String responseBody = EntityUtils.toString(response.getEntity());