喜逢身體我已經發送JSON對象到PHP服務器。我的JSON是如下─從Android的JSON發送到PHP服務器
{"cart_detail":{"product_id":"66922","ref_id":"chand09","user_id":"1"}}
我在下方發送JSON代碼 -
public void postData(String result,JSONObject obj) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
System.out.println(" before JSON string");
String json=obj.toString();
try {
HttpPost httppost = new HttpPost(result.toString());
httppost.setHeader("Content-type", "application/json");
System.out.println("After httpost header");
StringEntity se = new StringEntity(obj.toString());
System.out.println(" sending json string"+se);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String temp = EntityUtils.toString(response.getEntity());
System.out.println(" i am getting response entity "+entity);
Log.i("tag", temp);
System.out.println(" item send sucessfully to cart by server");
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
我的PHP處理響應是原樣
$data = array("cart_details" => array($_REQUEST));
echo "<pre>";
print_r($data);
echo "</pre>";
對此我得到堆棧跟蹤是爲後續
07-26 14:57:45.124: I/System.out(7191): i am getting response entity [email protected]
07-26 14:57:45.124: I/System.out(7191): i am getting response string [email protected]
07-26 14:57:45.124: I/tag(7191): <pre>Array
07-26 14:57:45.124: I/tag(7191): (
07-26 14:57:45.124: I/tag(7191): [cart_details] => Array
07-26 14:57:45.124: I/tag(7191): (
07-26 14:57:45.124: I/tag(7191): [0] => Array
07-26 14:57:45.124: I/tag(7191): (
07-26 14:57:45.124: I/tag(7191): )
07-26 14:57:45.124: I/tag(7191): )
07-26 14:57:45.124: I/tag(7191):)
07-26 14:57:45.124: I/tag(7191): </pre>
07-26 14:57:45.124: I/System.out(7191): item send sucessfully to cart by server
我不知道發生了什麼問題。我是json的新手。我將如何檢查我的要求是否與否。作爲迴應,我收到了同樣的json,但是爲什麼這個json是空的?是android部分還是PHP Server的錯。提前感謝大家。
嘗試var_dump($ _ REQUEST)如果這是空的,那麼你要麼不發送你的數據到服務器,要麼是沒有設置它 – Anigel
而不是jSon使用Http Post請求發送請求到PHP服務器,發送大塊的數據會很有用。 – onkar
@onkar ....是的,我使用..HttpPost httppost = new HttpPost(result.toString()); – DJhon