1
我想將數據作爲JSON從Android發佈到PHP腳本,但它似乎沒有讀取我發送的數據。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}
的Java
@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應用程序。
您是否錯過了'$ json = json_decode($ json);'在代碼中訪問它之前?什麼是'print_r($ json);' – lisachenko
@Alexander的輸出我實際上有更多的代碼將數據發佈到.txt文件中用於存儲目的(temp)。 – Xavier
你有什麼來自'file_get_contents('php:// input')'?如果你在這裏有一個正確的字符串,那麼問題出現在你的PHP代碼中,如果沒有,那麼問題出現在應用程序的Android部分。 – lisachenko