从android中获取服务器的响应

时间:2011-12-07 03:06:36

标签: android

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



        }

但我无法得到响应text.my php文件代码是这样的:

if(isset($_POST)){
$id=$_POST['id'];

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

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:7)

尝试在回复中使用 EntityUtil

  

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