我想發送一個簡單的JSON對象到PHP服務器,但是當我嘗試在服務器端檢索該對象時,沒有什麼我的意思是我的$ _POST變量是空的。服務器端是PHP 5.2,我使用的是Android模擬器10 ...有人可以看看我的代碼並告訴我發生了什麼問題嗎? 非常感謝如何將Android應用程序的JSONObject傳遞給PHP文件?
public void uploadJSon() throws ClientProtocolException, IOException, JSONException{ HttpClient httpclient = new DefaultHttpClient(); String url = "http://so-dev-deb.niv2.com/suivi_activite/test.php"; HttpPost httppost = new HttpPost(url); JSONObject json = new JSONObject(); json.put("username", "bob"); json.put("email", "[email protected]"); List nvps = new ArrayList(); nvps.add(new BasicNameValuePair("value", json.toString())); httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); URL test_url = new URL(url); URLConnection connection = test_url.openConnection(); connection.setDoOutput(true); HttpResponse response; response = httpclient.execute(httppost); Log.i("NVPS",nvps.get(0).toString()); Log.i("JSON",json.toString()); Log.i("response", response.getEntity().getContent().toString()); Log.i("response status",response.getStatusLine().toString()); BufferedReader in = new BufferedReader( new InputStreamReader( connection.getInputStream())); String decodedString; while ((decodedString = in.readLine()) != null) { //System.out.println(decodedString); Log.i("info 10",decodedString); } in.close(); }
服務器端test.php的是:
<?php
$tmp = json_decode($_POST['value']);
var_dump($tmp);
?>
我不知道爲什麼它沒有正確顯示在我的代碼中,但必須添加此行List nvps = new ArrayList ();而不是實際顯示的內容.... Thx –
user683831
2011-06-01 15:39:34