pojo class
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)
答案 0 :(得分:0)
将您的电话改为: 这是您的ApiInterface
@GET("articles")
retrofit2.Call<NewsResponse> getNews(@Query("source") String source,
@Query("sortBy") String sortBy,
@Query("apiKey") String apiKey);
来自您的活动:
ApiInterface apiInterface = APIClient.getClient().create(ApiInterface.class);
String source = "hacker-news";
String sortBy = "top";
String api_key = "687df54016f446be94f639d4cff8834f";
Call<NewsResponse> call = apiInterface.getNews(source,sortBy,api_key);
call.enqueue(new Callback<NewsResponse>() {
@Override
public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
if(response.body() != null){
Log.d(TAG,"Size of list is: "+response.body().getArticles().size());
}
}
@Override
public void onFailure(Call<NewsResponse> call, Throwable t) {
}
});