Android的HttpPost送空$ _ POST PHP脚本 [英] Android HttpPost send empty $_POST to a PHP script

查看:117
本文介绍了Android的HttpPost送空$ _ POST PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图从我的Andr​​oid应用程序发送POST请求的URL,但所有我得到的是一个空的$ _ POST。这里是我的code,它是一个AsyncTask的内部运行。我看着像这样在这里StackOverflow的几个问题,但没有人能帮助我。

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

在服务器端,我有一个PHP测试脚本测试如果$ _POST变量都OK了。

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"]); ?> 

所有我得到的是一个没有设置$ _ POST

All that I get is a not set $_POST

STATUS CODE 200
RETURN ISSET = 

我没有得到任何错误警告和App不会崩溃。

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

我试图把变量附加到URL中的GET请求,并改变了我的PHP文件来处理的$ _GET要求:

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");

这工作得很好。我可以阅读在PHP脚本都$ _GET变量。但我需要它是一个POST请求。

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

我在AndroidManifest.xml中INTERNET权限。在这个相同的应用程序我做的另一个PHP脚本的请求,并正常工作。

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" />

有什么想法?

推荐答案

如果您HttpPost URL有WWW删除它和尝试。如果不加WWW。

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

由于,你可能已经重定向的 yoursite.com www.yoursite.com 或反之亦然。在这样的情况下,你的HttpPost网址应的指定的URL,而不是重定向URL。这些参数将在HTTP重定向过程中丢失和空POST将完成。这就是为什么服务器没有收到您的文章的数据。

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.

这篇关于Android的HttpPost送空$ _ POST PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆