我使用http找到了PostData的this主题。但它不起作用。
怎么了?
我还要求Android.permission.INTERNET
中的manifest
。
public void postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:/mysite.com/postTest.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("pw", "12456"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
tv.setText(response.toString());
} catch (ClientProtocolException e) {
//
} catch (IOException e) {
//
}
}
修改
我的申请将以此消息终止:
Unfortunately PostDataProject has stopped.
答案 0 :(得分:2)
试试这个:
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = responce.getEntity();
// convert stream to String
BufferedReader buf = new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb1 = new StringBuilder();
String line = null;
while ((line = buf.readLine()) != null) {
sb1.append(line);
}
tv.setText(sb1.toString());
如果数据仅用于textview或小型,则可以直接设置为textview:
tv.setText(EntityUtils.toString(response.getEntity()));
并使用此:
HttpPost httppost = new HttpPost("http://mysite.com/postTest.php");
而不是
HttpPost httppost = new HttpPost("http:/mysite.com/postTest.php");
答案 1 :(得分:1)
尝试通过EntityUtils
http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html
tv.setText(EntityUtils.toString(response.getEntity()));