我正在尝试将数据作为 json 从 Android 发布到 PHP 脚本,但它似乎并没有读取我发送的数据。
PHP
header('Content-type: application/json');
$json = file_get_contents('php://input');
print_r($json);
数据
{"Date":"2012-05-31","Device_ID":"soaptester","Time":"15:22:59","Longitude":0.33,"Latitude":51.01}
爪哇
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = getSharedPreferences("SoapTester", Context.MODE_PRIVATE);
setContentView(R.layout.main);
String address = sp.getString("address", null);
if (address == null) {
sp.edit().putString("address", "http://192.168.0.8/lt_data/test.php").commit();
}
addressTextView = (EditText) findViewById(R.id.address);
addressTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
sp.edit().putString("address", String.valueOf(v.getText())).commit();
return true;
}
});
track = new LocationTrack();
track.setDate("2012-05-31");
track.setTime("15:22:59");
track.setLatitude(51.01);
track.setLongitude(0.33);
track.setDevice_ID("soaptester");
output = new Gson().toJson(track);
outputTextView = (TextView) findViewById(R.id.output);
outputTextView.setText(output);
}
@Override
protected void onResume() {
super.onResume();
addressTextView.setText(sp.getString("address", ""));
}
public void sendPost(View view) {
ServiceResponse r = Resting.post(sp.getString("address", ""), 80, output, EncodingTypes.UTF8);
((TextView)findViewById(R.id.response)).setText(String.valueOf(r));
}
经过大量测试后,我似乎无法找出为什么它没有从 android 应用程序接收数据。