我尝试使用Android将数据发布到PHP。但是在PHP中收到Null。这是我的代码......
Android代码:
try
{
JSONStringer().object()
JSONObject data = new JSONObject();
data.put("phone", "11");
data.put("pwd", "aa");
HttpPost request = new HttpPost("http://www.cslindia.org/viz/test.php");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(data.toString());
request.setEntity(entity);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity httpEntity =response.getEntity();
String json = EntityUtils.toString(httpEntity);
Log.e("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",json);
}
catch (Exception e)
{
e.printStackTrace();
}
PHP代码:
$json = file_get_content('php://input');
print_r("1".$json); //For Testing Purpose
$jsonObj = json_decode($json, true);
print_r("2".$jsonObj); //For Testing Purpose
$mphone=$jsonObj['phone'];
$mpwd=$jsonObj['pwd'];
但是,$ json,$ jsonObj都有空值。可能是什么解决方案请帮助我...