简体   繁体   中英

how to parse json from a url using volly

With below code i tried to parse json text from a url using Volly. but instead of giving the result it giving the some exception

2019-03-21 09:47:29.138 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 09:57:20.919 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 09:57:21.522 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 09:57:25.578 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:57:32.832 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:57:32.879 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:09.997 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:10.084 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:12.130 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:12.156 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:13.473 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:13.508 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:16.968 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
2019-03-21 09:58:16.993 21642-21642/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.widget.ImageView.getDrawable()' on a null object reference
2019-03-21 09:58:33.128 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
2019-03-21 10:02:29.501 8603-8603/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

below is the json structure.

{"ip":"111.125.204.140","type":"ipv4","continent_code":"AS","continent_name":"Asia"}

from above json text i want to fetch type value. below are my code.

 RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://api.ipstack.com/111.125.204.140?access_key=##########";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    JSONObject json = null;
                    try {
                        json = new JSONObject(response);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    String cityName = json.optString("type");// here iam not getting any value.
                    txtJson.setText(cityName);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            txtJson.setText("That didn't work!");
        }
    });

    queue.add(stringRequest);

You got NullPointerException because your json variable is null at this line:

String cityName = json.optString("type");

You need to check that is null or not before you use it.

I think you got an exception when you try to parse the response, but you catch it and simply log it, but you don't handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM