0

我使用 http 找到了 PostData这个主题。但它不起作用。

怎么了?

我也在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.

4

2 回答 2

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());

如果数据仅用于 textvew 或较小,您可以直接设置为 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");
于 2012-04-07T10:24:02.510 回答
1

尝试通过EntityUtils http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.html获得响应

 tv.setText(EntityUtils.toString(response.getEntity()));
于 2012-04-07T10:20:53.883 回答