2011-08-28 39 views
1

我已經做原料得到休息和崗位,也可以做後在的NameValuePair但無論我遵循的教程中,我只是不能得到JSON後做...Android的JSON後到PHP

Android應用程序:

ArrayList<String> stringData = new ArrayList<String>(); 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost postMethod = new HttpPost("http://192.168.1.10/people.php");  

JSONObject holder = new JSONObject(); 
holder.put("name", "Foo");  
holder.put("occupation", "Bar"); 

StringEntity se = new StringEntity(holder.toString()); 
postMethod.setEntity(se); 
postMethod.setHeader("Accept", "application/json"); 
postMethod.setHeader("Content-type", "application/json"); 

ResponseHandler <String> resonseHandler = new BasicResponseHandler(); 
String response=httpClient.execute(postMethod,resonseHandler); 
stringData.add(response); 

return stringData; 

PHP文件:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 

var_dump($_POST); 
} 

後續代碼var_dump只是給出了一個空數組和$ _ POST [ 「名稱」]給出不確定的指數...任何幫助將是不錯的讀取POST數據

回答

2

你試過裝運holder.toString()BasicNameValuePair類?

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("myjson", holder.toString()); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

然後就在你的PHP你得到它像

$jsonstring = $_REQUEST['myjson']; 
+0

也許你需要在你的php代碼中解碼它。如果需要解碼,請使用'$ jsonstring = urldecode($ _ REQUEST ['myjson'];' –

+0

這個答案對我有用:) – code4j

0

我認爲這個問題可能是你正在使用的URL。

發佈到10.0.2.2而不是您在http post中指定的發佈。