如何使用volly从URL解析json

时间:2019-03-21 04:27:07

标签: java android json parsing android-volley

使用以下代码,我尝试使用Volly从URL解析json文本。但没有给出结果,而是给出了一些异常

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

下面是json结构。

{"ip":"111.125.204.140","type":"ipv4","continent_code":"AS","continent_name":"Asia"}
从上面的json文本

我想获取类型值。下面是我的代码。

 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);

1 个答案:

答案 0 :(得分:0)

您得到NullPointerException是因为您的json变量在此行为空:

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

在使用它之前,您需要检查它是否为空。

我认为您尝试解析响应时遇到了一个异常,但是您捕获了该响应并只是将其记录下来,但是您不处理它。