【问题标题】:how to parse json from a url using volly如何使用 volly 从 url 解析 json
【发布时间】:2019-08-11 21:59:33
【问题描述】:

通过下面的代码,我尝试使用 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);

【问题讨论】:

  • 哪一行出现错误,发布您的完整堆栈跟踪
  • 先检查你的回复是否为空。
  • 您需要包含堆栈跟踪。在这种情况下,异常消息是不够的。
  • 我编辑了问题,请看一下
  • 调试并检查您是否在响应对象中获取值。

标签: java android json parsing android-volley


【解决方案1】:

你得到了NullPointerException,因为你的json变量在这一行是空的:

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

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

我认为您在尝试解析响应时遇到了异常,但您捕获它并简单地记录它,但您不处理它。

【讨论】:

  • 是的,我尝试捕获响应对象,它没有给出任何值。但我不明白到底是什么问题
  • 也许你得到的是 JSONArray 而不是 JSONObject?
  • 那我将如何获得 JSONObject?
  • 就像从数组中获取对象一样。
猜你喜欢
  • 2015-08-12
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多