Load Image in ImageView from URL stored in JSONString in Android

前端 未结 5 692
梦如初夏
梦如初夏 2021-01-13 13:31

I have a JSON string, say a name and a Url . I need to extract name into the TextView and extract and display image in ImageView. Belo

5条回答
  •  囚心锁ツ
    2021-01-13 14:19

    You could extract the image name and url like this

     public static final String JSON_STRING="{\"WebImages\":{\"Imagename\":\"image_name\",\"imageurl\":http://www.example.com/image/example.png}}"; 
    JSONObject jsonObject = new JSONObject(JSON_STRING);
    JSONObject webImages = jsonObject.getJSONObject("WebImages");
    String imageName = webImages.getString("Imagename");
    String imageUrl = webImages.getString("imageurl");
    

    Now, you have the imageName and the imageUrl. You can easily set the text doing something like myTextView.setText(imageName). For loading the image into ImageView, I suggest using the Picasso library. It is extremely easy to use. All you need is one line of code, which would look like

    Picasso.with(context).load(imageUrl).into(imageView);
    

提交回复
热议问题