使用 JSON 从 URL 添加图像
Posted
技术标签:
【中文标题】使用 JSON 从 URL 添加图像【英文标题】:Add Images from a URL using JSON 【发布时间】:2020-09-29 14:30:54 【问题描述】:我是 JSON 新手。我会感谢你帮助我。如何通过 Json 在图像中添加解析。有人可以告诉我我在这里想念什么吗?我希望每个新闻链接的图像都显示在我的ImageView
(xml 中的占位符)上,这里是链接和 api。
https://newsapi.org/v2/top-headlines?country=us&apiKey=6a6f7c77766442acb20c86157a152131"
非常感谢您的帮助
这是我在 Query.Java 中的代码
try
JSONObject baseJsonResponse = new JSONObject(bookJson);
JSONArray newsArray = baseJsonResponse.getJSONArray("articles");
for (int i = 0; i < newsArray.length(); i++)
JSONObject currentNews = newsArray.getJSONObject(i);
/*JSONObject properties = currentNews.getJSONObject("articles");*/
JSONObject newsSource = currentNews.getJSONObject("source");
String title = currentNews.getString("title");
String description = currentNews.getString("description");
String url = currentNews.getString("url");
/*String name = properties.getString("name");*/
String name = newsSource.getString("name");
String time = currentNews.getString("publishedAt");
String image = currentNews.getString("urlToImage");
News news = new News (title, description, url, name, time, image);
这是自定义类中的代码
package com.example.trynews;
public class News
private String mTitle;
private String mDescription;
private String mUrl;
private String mSource;
private String mTime;
private String mUrlImage;
public News (String title, String description, String url, String name, String time, String images)
mTitle = title;
mDescription = description;
mUrl = url;
mSource = name;
mTime = time;
mUrlImage = images;
public String getTitle()
return mTitle;
public String getDescription()
return mDescription;
public String getURL()
return mUrl;
public String getName()
return mSource;
public String getTIME()
return mTime;
public String getImageUrl()
return mUrlImage;
这是适配器(列表视图)中的代码
public class NewsAdapter extends ArrayAdapter<News>
public NewsAdapter(Context context, List<News> newss)
super(context, 0, newss);
@Override
public View getView(int position, View convertView, ViewGroup parent)
View listItemView = convertView;
if (listItemView == null)
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.news_list_item, parent, false);
News currentNews = getItem(position);
TextView titleView = (TextView) listItemView.findViewById(R.id.title);
titleView.setText(currentNews.getTitle());
TextView descripView = (TextView) listItemView.findViewById(R.id.description);
descripView.setText(currentNews.getDescription());
TextView sourceView = (TextView) listItemView.findViewById(R.id.newsSource);
sourceView.setText(currentNews.getName());
TextView timeView = (TextView) listItemView.findViewById(R.id.date);
timeView.setText(currentNews.getTIME());
ImageView imageView = (ImageView) listItemView.findViewById(R.id.imageView);
imageView.setImagerResource(currentNews.getImageUrl());
I am getting an error here it says I cant add a String inside setImageResource.
return listItemView;
【问题讨论】:
显示您的 api 响应。 json 【参考方案1】:如果你想从 url 显示图片,简单的方法是使用图片库。
例如Glide
添加 build.gradle
repositories
mavenCentral()
google()
dependencies
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
在你的适配器中,
imageView.setImagerResource(currentNews.getImageUrl());
改成
Glide.with(getContext).load(currentNews.getImageUrl()).into(imageView);
或者使用位图
Bitmap bitmap; // this is global
Thread mthread = new Thread()
@Override
public void run()
try
URL url = new URL(currentNews.getImageUrl());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
;
mthread.start();
try
mthread.join();
imageView.setImageBitmap(bitmap);
catch (InterruptedException e)
e.printStackTrace();
【讨论】:
它就像一个魅力非常感谢你帮助我【参考方案2】: ImageView imageView = (ImageView) listItemView.findViewById(R.id.imageView);
URL url = new URL(currentNews.getImageUrl());
Bitmap bitmap =
BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bitmap);
试试上面的代码
【讨论】:
【参考方案3】:使用 picasso 库从 URL 加载图像:如果您愿意,请点击以下链接:https://medium.com/@prakash_pun/retrofit-a-simple-android-tutorial-48437e4e5a23
【讨论】:
以上是关于使用 JSON 从 URL 添加图像的主要内容,如果未能解决你的问题,请参考以下文章
Swift 3将图像从URL添加到UIImageView [重复]