0

I am getting json string from my android app that contains the data for user registration. In server side i am using php to insert it in to database.

Here is the json string that I will get

{"user_name":"Steve Jobs","user_email":"[email protected]","user_password":"nopassword","user_phone_number":"1234567890","club_member_id":"24"}

But when I convert it in to an array I will get null value.

<?php

  //$_POST contains the json string. 
  $data = json_decode($_POST,true);


?>

$data will get empty string

How will I resolve it.

UPDATE

Content of file_get_contents('php://input')

POST=%7B%22user_name%22%3A%22Steve+Jobs%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();
        }

    }
3
  • 2
    Can you do a var_dump($_POST); ? Post the results here. Commented Jan 7, 2016 at 10:08
  • which variable within $_POST did you put the JSON string into? Commented Jan 7, 2016 at 10:11
  • how are you converting it to array? can u show the code for that Commented Jan 7, 2016 at 10:11

4 Answers 4

2

Having seen you edit, it seems you are sending the json as part of a key value pair after all, just with a weird key name (POST) which confused matters.

You should be able to get the data via:

$data = json_decode($_POST['POST'],true);

For clarity, it would probably make sense to change the key name to something more sensible:

nameValuePairs.add(new BasicNameValuePair("json", jsonParams.toString()));

Then you would access via:

$data = json_decode($_POST['json'],true);

Or alternativly, just send the data as json, not key value pair:

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");
        StringEntity params = new StringEntity(jsonParams.toString());
        httppost.addHeader("content-type", "application/json");
        httppost.setEntity(params);
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    }

Then use the php code described below in my original answer

ORIGINAL ANSWER:

$_POST is an array, populated automatically when a post request is received containing key value pairs (contentType application/x-www-form-urlencoded).

If you post json data (contentType application/json), $_POST will not be populated, instead you need to parse the input stream:

$data = json_decode(file_get_contents('php://input'), true);
Sign up to request clarification or add additional context in comments.

Comments

0

your json data might comming in some array index of POST not in complete global array you code should be like this

$data = json_decode($_POST['some_json_field'], true);

Comments

0

Try this:

$data = json_decode($_POST['string_name_in_which_json_string_is_present'],true);

1 Comment

This will get an empty array
0

you can't use $_POST to get the values. Please try:

$data = json_decode(file_get_contents('php://input'), true);

This should do it.

$_POST will not be populated if the request body is not in the standard urlencoded form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.