我有一個Android應用程序發送一個JSON字符串到我的服務器,它的格式如下:遇到問題解析JSON與PHP
{"drink_name":"testing","phone_number":"5555555555"}
當我使用命令:SELECT * FROM訂單,這表明,空白條目被插入到表中。
我認爲我的問題是由我的PHP腳本引起的(主要是因爲我是PHP新手)。 我正確解析json嗎? 下面是我寫的腳本。編輯: 這是我的Android代碼,如果它有任何幫助。
protected void sendJson(final String phnNmbr, final String drink) {
Thread t = new Thread(){
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost("http://kubie.dyndns-home.com/R2Bar2/sendOrder.php");
json.put("phone_number", phnNmbr);
json.put("drink_name", drink);
StringEntity se = new StringEntity("orders: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
Toast.makeText(getApplicationContext(), json.toString(), Toast.LENGTH_LONG).show();
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
}
}
catch(Exception e){
e.printStackTrace();
//createDialog("Error", "Cannot Estabilish Connection");
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
那麼,'var_dump($ obj)'給你什麼? – deceze 2012-04-02 06:45:27
另外,Android應用程序如何發送數據?請求是POST還是GET? – 2012-04-02 06:48:01
嘗試開啓錯誤報告/錯誤記錄並再次檢查輸出。 – 2012-04-02 06:53:22