我想发送一个简单的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