在Android中从服务器获取响应

3
    HttpClient httpclient=new DefaultHttpClient();  
            HttpPost httpPost=new HttpPost("http://10.0.2.2/android_post/post.php");
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id","manoj"));
                nameValuePairs.add(new BasicNameValuePair("password", " "));
                nameValuePairs.add(new BasicNameValuePair("action", "validate_password"));
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    Log.i("LEE", "cannot connect to server");
                }

                try {
                    HttpResponse response = httpclient.execute(httpPost);
                    String t=response.getParams().toString();


                    TextView tview=(TextView) findViewById(R.id.textView1);
                  tview.setText(t);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



        }

但是我无法获得响应文本。我的PHP文件代码如下:
if(isset($_POST)){
$id=$_POST['id'];

print "Hi! I \'m the server";
}

有人能帮我吗?

这个问题与IT技术有关。
1个回答

7
尝试在响应中使用EntityUtil

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


@Parth Doshi 很好的回答。 - ranjjose

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接