【问题标题】:PHP $_POST data comes up empty AndroidPHP $_POST 数据出现空 Android
【发布时间】:2012-12-13 12:37:32
【问题描述】:

我正在开发一个 Android 应用程序,该应用程序读取在线 PHP 脚本并回显具有不同结果的 JSON 数组。我还将应用程序中的 POST 变量发送到 PHP 脚本中。当我从网站发布到脚本时,它工作正常并且 POST 数据存在。当我从应用程序发布时,POST 数据为空。

private JSONArray addBusiness(String businessName, String businessCategory, String businessDescription) {
    String result = "";
    InputStream is = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://somesite.com/testAndroid.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    try {
        Log.e("log_tag", "INSIDE TRY/CATCH");
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("log_tag", "RESULT: " + result);
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }

    try {
        return new JSONArray(result);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    return null;
}

这是空的 PHP 代码。有任何想法吗?另外,我使用运行 PHP 5.2 的 GoDaddy 共享 Linux 主机

<?php
$return = array();
$return['result'] = 'pooping for the Internet';
$return['business'] = 'BUSINESS';
$return['post'] = $_POST['id'];
echo '['.json_encode($return).']';
?>

所有其他 $return 数据都是正确的,除了 'post' return。想法?

【问题讨论】:

  • 如果剩下的问题来了,问题似乎出在您的$_POST 中。确保您的发布正确。尝试测试帖子是否正常通过

标签: php android post


【解决方案1】:

您的应用程序是否生成 PHP 代码结果的 HTTP 请求?

您可以像使用 Perl 或 Python 一样解释 PHP (CLI-Mode),如果您这样做,则不涉及任何 HTTP-Protocoll。如果您的脚本是通过网络服务器交付的,那么确实应该有 $_POST、$_GET 和 $_REQUEST 数组。

您可以使用this检查是否有http-header!

【讨论】:

    【解决方案2】:

    感谢您提供的所有提示。问题最终出现在我的.htaccess 文件中。我忘了我有一个重写规则,将“www”放在 url 前面。我试图在没有开头“www”的情况下发布到我的网站,这会导致 POST 数据丢失。我覆盖了正在工作的子目录的.htaccess 文件,现在一切正常!!!再次感谢您的提示!

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 2015-05-12
      • 1970-01-01
      • 2012-09-07
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2015-01-11
      相关资源
      最近更新 更多