2017-07-08 32 views
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) 
+0

你有什麼迴應嗎?通過打電話給這個API,我的意思是你打電話的方式? – Ashwani

+0

主要錯誤是您撥打電話 – Ashwani

+0

感謝您的代碼它對我有用 – JRaj

回答

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) { 

      } 
     });