简体   繁体   中英

Image URL using Jsoup is not getting attached to NetworkImageView

In Json request, I am getting Image URL for particular article but NetWorkImageView is not getting that Image URL. Here is my request:

JsonObjectRequest objectRequest = new JsonObjectRequest(url,null,new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {

                hidePDialog();
                JSONObject rss = response.getJSONObject("rss");
                JSONObject channel = rss.getJSONObject("channel");
                JSONArray item = channel.getJSONArray("item");
                Log.d(TAG, item.toString());
                for (int i = 0; i < item.length(); i++) {


                    JSONObject obj = item.getJSONObject(i);
                    Movie movie = new Movie();
                    movie.setTitle(obj.getString("title"));
                    //Image Url
                    String imageLink = obj.getString("description");
                    Document doc = Jsoup.parse(imageLink);
                    Element link = doc.select("img").first();
                    //System.out.println(link.absUrl("src"));

                    //ERROR comes with line below.
                    movie.setThumbnailUrl(link.absUrl("src"));
                    movieList.add(movie);

                }



            } catch (JSONException e) {
                e.printStackTrace();
            }
            adapter.notifyDataSetChanged();
        }

    },new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            VolleyLog.d(TAG, "Error: " + volleyError.getMessage());
            hidePDialog();
        }
    });
 AppController.getInstance().addToRequestQueue(objectRequest);
    return rootView;
}

It looks complete but I am still making silly mistake here. Getting silly NullPointerException . I trying it for some time now but stuck here for while now. Error is small but unable to find it though.

If you need more code snippets then let me know. Your help will be very much appreciated. Sorry for dumb question.

EDIT 1 Logcat Logcat

如果您将第76行标记为正确-您的问题的答案是该链接为null,因为它不存在于json字符串中。

Instead of

               Element link = doc.select("img").first();
               movie.setThumbnailUrl(link.absUrl("src"));

I should have used:

                    Elements link = doc.select("img");
                    if(link.attr("src")!= null)
                    {
                        movie.setThumbnailUrl(link.attr("src"));
                    }
                    else {
                        movie.setThumbnailUrl(null);
                    }

Link was not null. My approach was wrong.

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