1

I want to get the value of JSONObject in android using volley library.

Here is my MainActvity.java code.

 String url = "http://someexample.com/signin.php";
        StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response).getJSONObject("type");
                            String myObjAsString = jsonResponse.toString();

                            mTV.setText(myObjAsString.toString());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                // the POST parameters:
                params.put("email", "example@outlook.com");
                params.put("password", "********");
                return params;
            }
        };
        Volley.newRequestQueue(this).add(postRequest);

Here is my web service response.

{
   "type":"1",
   "data":{
      "userID":"197",
      "name":"Hasnain",
      "email":"example@outlook.com",
      "paymentstatus":1
   },
   "title":"Message",
   "message":"User loged in successfully",
   "_explicitType":null
}

Here is my error stack .

09-08 11:22:21.068    4705-4825/com.example.alphabooksingintest D/OpenGLRenderer﹕ Enabling debug mode 0
09-08 11:22:21.135    4705-4742/com.example.alphabooksingintest W/art﹕ Suspending all threads took: 18.063ms
09-08 11:22:21.212    4705-4705/com.example.alphabooksingintest W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
09-08 11:22:30.150    4705-4742/com.example.alphabooksingintest W/art﹕ Suspending all threads took: 16.658ms
09-08 11:22:30.918    4705-4705/com.example.alphabooksingintest W/System.err﹕ org.json.JSONException: Value 1 at type of type java.lang.String cannot be converted to JSONObject
09-08 11:22:30.926    4705-4705/com.example.alphabooksingintest W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:613)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.example.alphabooksingintest.MainActivity$1.onResponse(MainActivity.java:42)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.example.alphabooksingintest.MainActivity$1.onResponse(MainActivity.java:38)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5312)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
09-08 11:22:30.927    4705-4705/com.example.alphabooksingintest W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
09-08 11:23:37.309    4705-4742/com.example.alphabooksingintest W/art﹕ Suspending all threads took: 5.080ms

I want to get the value of “type” object from the response.

hasnain_ahmad
  • 325
  • 6
  • 17
  • "type" is not a JSON object. it is string look at the Rajesh jadav answer – Amol Sawant Sep 08 '15 at 06:32
  • I am new to json. @AmolSawant96Kuli if you check my json reponse in this website http://www.jsoneditoronline.org/ it shows my response has five objects including "type". – hasnain_ahmad Sep 08 '15 at 06:47

3 Answers3

5

Error:

org.json.JSONException: Value 1 at type of type java.lang.String cannot be converted to JSONObject

From error log i can see that you are trying to convert JSONObject into String so you are getting JSONException.

You need to get type String value from JSONObject this way.

Try this code:

JSONObject jsonResponse = new JSONObject(response);
String myObjAsString = jsonResponse.getString("type");

instead of

JSONObject jsonResponse = new JSONObject(response).getJSONObject("type");
String myObjAsString = jsonResponse.toString();

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
1

Get your JSONObject with "type"

try {
     JSONObject jsonResponse = new JSONObject(response);
     String myObjAsString = jsonResponse.getString("type");
     mTV.setText(myObjAsString.toString());
}catch(JSONException e) {
  e.printStackTrace();
}
Iamat8
  • 3,888
  • 9
  • 25
  • 35
0

Its because you want a JSON object as a response but still you are using String request.Try JSON Object request in place of JSON String request.It will work

Hosein Hamedi
  • 322
  • 4
  • 16
Animesh Jena
  • 1,541
  • 1
  • 25
  • 44