我從我的android應用程序中獲取包含用戶註冊數據的json字符串。 在服務器端,我使用PHP將其插入數據庫。json_decode返回nempty值php
這裏是JSON字符串,我會得到
{"user_name":"Steve Jobs","user_email":"[email protected]","user_password":"nopassword","user_phone_number":"1234567890","club_member_id":"24"}
但是,當我把它轉換到一個數組我會得到空值。
<?php
//$_POST contains the json string.
$data = json_decode($_POST,true);
?>
$data
會得到空字符串
我將如何解決這個問題。
UPDATE
內容file_get_contents('php://input')
POST =%7B%22user_name%22%3A%22Steve +作業%22%2C%22user_email%22%3A%22steave%40apple.com的%22%2C%22user_password%22%3A%22nopassword%22%2C%22user_phone_number%22%3A%221234567890%22%2C%22club_member_id%22%3A%2224%22%7
JAVA
個private void onSignup() throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
// Add your data
try {
jsonParams.put("user_name", "Steve Jobs");
jsonParams.put("user_email", "[email protected]");
jsonParams.put("user_password", "nopassword");
jsonParams.put("user_phone_number", "1234567890");
jsonParams.put("club_member_id", "24");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("POST", jsonParams.toString()));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
// Use UrlEncodedFormEntity to send in proper format which we need
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (JSONException e) {
}
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
Log.e("Data", String.valueOf(json));
try {
// Getting JSON Array
JSONArray user = json.getJSONArray("POST");
Log.e("JSON ARRAY", String.valueOf(user));
ArrayList<String> stringArray = new ArrayList<String>();
for (int i = 0, count = user.length(); i < count; i++) {
try {
JSONObject jsonObject = user.getJSONObject(i);
String name = jsonObject.getString(TAG_NAME);
stringArray.add(name);
Log.e("JSON ARRAY", String.valueOf(stringArray));
} catch (JSONException e) {
e.printStackTrace();
}
}
// name1.setText(String.valueOf(stringArray));
} catch (JSONException e) {
e.printStackTrace();
}
}
你可以做'var_dump($ _ POST);'?在此發佈結果。 – Daan
你將JSON字符串放入$ _POST中的哪個變量? –
你如何將它轉換爲數組?你可以顯示該代碼 –