我正在開發一個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代碼。有任何想法嗎?另外,我使用GoDaddy的共享Linux主機上運行PHP 5.2
<?php
$return = array();
$return['result'] = 'pooping for the Internet';
$return['business'] = 'BUSINESS';
$return['post'] = $_POST['id'];
echo '['.json_encode($return).']';
?>
所有其他$返回的數據是正確的,除了「後的迴歸。思考?
如果剩下的問題出現在你的'$ _POST'中。確保您的發佈正確。嘗試測試帖子是否正確通過 – cjds