PHP $ _POST数据显示为空Android [英] PHP $_POST data comes up empty Android

查看:68
本文介绍了PHP $ _POST数据显示为空Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an Android app that reads an online PHP script and echos back a JSON array with different results. I am also sending POST variables from the app into the PHP script. When I post to the script from the website, it works fine and the POST data is present. When I post from the app, the POST data is empty.

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

Here is the PHP code that comes up empty. Any ideas? Also, I use GoDaddy Shared Linux Hosting running PHP 5.2

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

除"post"返回外,所有其他$ return数据都是正确的.有想法吗?

All other $return data is correct except the 'post' return. Thoughts?

推荐答案

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

Thank you for all the tips. The problem ending up being in my .htaccess file. I forgot I had a rewrite rule that place "www" in front of the url. I was trying to POST to my site without the beginning "www"s which caused the lose in POST data. I overwrote the .htaccess file for the sub-directory in was working in and now everything works great!!! Thanks again for the tips!

这篇关于PHP $ _POST数据显示为空Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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