我正在开发一个读取在线 PHP 脚本并回显具有不同结果的 JSON 数组的 Android 应用程序。我还将应用程序中的 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' 回报。想法?