0
我使用這個API: https://newsapi.org/v1/articles?source=hacker-news&sortBy=top&apiKey=687df54016f446be94f639d4cff8834f如何在我的PGM中加載圖片和作者姓名?
POJO類
public class News {
@SerializedName("author")
String title;
@SerializedName("urlToImage")
String urlToImage;
public News(String title, String urlToImage) {
this.title = title;
this.urlToImage = urlToImage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
}
列表
public class NewResponse {
@SerializedName("articles")
private List<News> newsList;
public List<News> getNewsList() {
return newsList;
}
}
API接口
我覺得問題就在這裏,但我不知道
public interface ApiInterface {
@GET("articles?source=hacker-news&sortBy=top&")
retrofit2.Call<NewResponse> getNews(@Query("apiKey") String apiKey);
}
網址
和這裏
public class Constants {
public final static String NEWS_API_KEY = "687df54016f446be94f639d4cff8834f";
public final static String NEWS_API_URL = "https://newsapi.org/";
}
適配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context mContext;
private List<News> newsList;
public RecyclerAdapter(Context mContext, List<News> newsList) {
this.mContext = mContext;
this.newsList = newsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Context context=viewGroup.getContext();
LayoutInflater inflater=LayoutInflater.from(context);
View news=inflater.inflate(R.layout.newlayout,viewGroup,false);
return new MyViewHolder(news);
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
News news=newsList.get(i);
TextView nTitle=myViewHolder.newsTitle;
nTitle.setText(news.getTitle());
// Glide.with(mContext).load(news.getPoster_path()).into(myViewHolder.newsThumbnail);
Picasso.with((mContext)).load(news.getUrlToImage()).fit().into(myViewHolder.newsThumbnail);
}
@Override
public int getItemCount() {
return newsList.size();
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
recyclerView.clearAnimation();
super.onDetachedFromRecyclerView(recyclerView);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView newsTitle;
private ImageView newsThumbnail;
public MyViewHolder(View itemView) {
super(itemView);
newsThumbnail = (ImageView) itemView.findViewById(R.id.imageView);
newsTitle = (TextView) itemView.findViewById(R.id.textView2);
}
}
}
改造
public class NewsAPI {
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(Constants.NEWS_API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
logcat的
07-15 15:20:21.719 11101-11101/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.jmzala.recyclewithjson.MainActivity$1.onResponse(MainActivity.java:75)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5409)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
你有什麼迴應嗎?通過打電話給這個API,我的意思是你打電話的方式? – Ashwani
主要錯誤是您撥打電話 – Ashwani
感謝您的代碼它對我有用 – JRaj