Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

Android HttpPost send empty $_POST to a PHP script

I have been trying to send a POST request to a URL from my Android App, but all that I get is a empty $_POST. Here is my code, it is running inside a Asynctask. I looked at several issues like this here in the StackOverflow, but none of them could help me.

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.somesite.com/clients/app-controller/posts.php");

try {
      List<NameValuePair> pairs = new ArrayList<NameValuePair>();
      pairs.add(new BasicNameValuePair("id", "12345"));
      pairs.add(new BasicNameValuePair("name", "John"));
      pairs.add(new BasicNameValuePair("post", "test123"));
      post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));

      post.setHeader("Content-Type", "application/x-www-form-urlencoded");

      HttpResponse response = httpClient.execute(post);

      String text = EntityUtils.toString(response.getEntity());
      System.out.println("STATUS CODE " +  response.getStatusLine().getStatusCode());
      System.out.println("RETURN " +  text);

} catch (ClientProtocolException e) {
      System.out.println("ERROR1: " +  e.getStackTrace());
} catch (IOException e) {
      System.out.println("ERROR2: " + e.getStackTrace());
}

On the server side, I have a PHP test script testing if the $_POST variable are OK.

<?php echo "ISSET = " . isset($_POST["id"]) . isset($_POST["name"]) . isset($_POST["post"]); ?> 

All that I get is a not set $_POST

STATUS CODE 200
RETURN ISSET = 

I don't get any error warning and the App doesn't crash.

I tried to put the variables as a GET request attached to the URL and changed my PHP file to process the the $_GET request:

HttpPost post = new HttpPost("http://www.somesite.com/clients/app-controller/posts.php?id=1234&name=John&post=test123");

It worked fine. I could read all $_GET variables in the PHP script. But I need it to be a POST request.

I have the INTERNET permission in my AndroidManifest.xml. In this same App I do requests for another PHP script and it work as expected.

<uses-permission android:name="android.permission.INTERNET" />

Any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your HttpPost URL has "www" remove it and try. If not add www.

Because, you might have redirected yoursite.com to www.yoursite.com or vice versa. In such cases, your HttpPost URL should be the designation URL and not the redirecting URL. The parameters will be lost during the HTTP redirection process and a empty POST will be done. This is why the server is not receiving your post data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...