PHP $ _POST数据出现空Android

时间:2012-12-28 07:36:58

标签: php android post

我正在研究一个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 Shared Linux Hosting

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

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

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

您可以使用this检查是否有http标头!