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";
}
任何人都可以帮助我吗?
答案 0 :(得分:7)
尝试在回复中使用 EntityUtil :
String t = EntityUtils.toString(response.getEntity());