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またはsmallのみの場合、uは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

http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/util/EntityUtils.htmlで応答を取得してみてくださいEntityUtils

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