0

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

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

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

Tim Kipp
  • 200
  • 1
  • 16
  • If the rest is coming the problem looks to be in your `$_POST`. Make sure your posting properly. Try testing if the Post is coming through properly – cjds Dec 28 '12 at 07:56

2 Answers2

2

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 loss 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!

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Tim Kipp
  • 200
  • 1
  • 16
0

Does your App generate HTTP-Requests of the Result of the PHP-Code?

You can interpret PHP (CLI-Mode) just as you might use Perl or Python, if you do so you don't have any HTTP-Protocoll involved. If your Script is delivered via a webserver there should indeed be $_POST,$_GET and $_REQUEST Arrays.

You can check using this whether there is a http-header or not!

cjds
  • 8,268
  • 10
  • 49
  • 84
wegus
  • 282
  • 1
  • 9