Android: Cannot get the httpPost params but can get the httpGet from php
- by jjLin
Here is my android code to send request:
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serverUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("abc", "abc2"));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = null;
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String json = "";
json = sb.toString();
Log.d("JSON", "JSON is:" + json);
and here is my php code to get the request:
<?php
echo $_POST['abc'];
?>
When I run the application, the string json is nothing. I expect to get JSON is:abc2
Then I change the some code, in android part:
HttpPost httpPost = new HttpPost(serverUrl);
change to:
HttpPost httpPost = new HttpPost(serverUrl + "?abc=abc3");
in php part:
<?php
echo $_GET['abc'];
?>
This time, the string json in logcat is JSON is:abc3. It is correct!!
I have tried lots of time, but seems cannot send HttpPost request with params.
Any one can help me to find out what wrongs with my code??