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?