*我是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);
here is the code in custom class
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;
}
}
and here is the code in adapter (listview)
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;
}
ImageView imageView = (ImageView) listItemView.findViewById(R.id.imageView);
URL url = new URL(currentNews.getImageUrl());
Bitmap bitmap =
BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bitmap);
尝试上面的代码
如果要显示来自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);