Android: Cannot get the httpPost params but can get the httpGet from php

Posted by jjLin on Stack Overflow See other posts from Stack Overflow or by jjLin
Published on 2012-07-06T03:11:21Z Indexed on 2012/07/06 3:15 UTC
Read the original article Hit count: 321

Filed under:

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??

© Stack Overflow or respective owner

Related posts about android