0

I have a Json reponse like this:

{
    "status" : "CREATED",
    "message" : "user created",
    "results" : [
        {
            "id" : "1243233",
        }
    ]
}

The above JSON response will be obtained with a POST call.In android using volley library i did a REST call as following:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                                url, null,
                                new Response.Listener<JSONObject>() {

                                    @Override
                                    public void onResponse(JSONObject response) {
                                        Log.d("Request", response.toString());
                                        try {
                                            response.getString("status");
                                            JSONArray array = response.getJSONArray("results");

                                                JSONObject id = (JSONObject)array.get(0);




                                            Toast.makeText(getApplicationContext(),response.getString("status"),Toast.LENGTH_LONG).show();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        pDialog.hide();

                                    }
                                }, new Response.ErrorListener() {

                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d("Request", "Error: " + error.getMessage());

                                pDialog.hide();
                            }
                        }){
                            @Override
                            public Map<String, String> getHeaders() throws AuthFailureError {
                                HashMap<String, String> headers = new HashMap<String, String>();
                                headers.put("Content-Type", "application/json");
                                return headers;
                            }

                            @Override
                            protected Map<String, String> getParams() throws AuthFailureError {
                                Map<String, String> params = new HashMap<String, String>();
                                params.put("phone", phone);
                                params.put("name", username);
                                params.put("pwd",password);
                                params.put("email", email);
                                return params;
                            }
                        };

                        AppController.getInstance().addToRequestQueue(jsonObjReq);

I am getting an error org.json.JSONException: Expected literal value at character 102 of

 {
    "status" : "CREATED",
    "message" : "user created",
    "results" : [
        {
            "id" : "1243233",
        }
    ]
}

Please help me to solve this.

4
  • Your json is not valid Take a look here "id" : "1243233", - you have to remove coma on the end of this line. Commented May 8, 2015 at 11:40
  • @Manikanta android volley to better to user asynctask httpclient its so easy and speed to given responces Commented May 8, 2015 at 12:20
  • @Hardik Parmar you want me to use Async Task for background tasks instead of Volley? Commented May 9, 2015 at 12:05
  • @Manikanta you check this link for more information about async httpclient "loopj.com/android-async-http" Commented May 11, 2015 at 4:09

1 Answer 1

1

There is a comma in the results array's item. Which means there should be another element(string,integer etc) as part of that JSONObject(first item of results array).

Hence you get the JSON parsing error.

    "results" : [
                   {
                     "id" : "1243233",
                    }
                ]

should be

   "results" : [
                 {
                      "id" : "1243233"
                 }
               ]

Removing comma

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, i am breaking my head from one hour :)

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.