使用Retrofit和Glide下载图像。

huangapple go评论108阅读模式
英文:

Download an image using Retrofit and Glide

问题

  1. // MainActivity:
  2. public class MainActivity extends AppCompatActivity {
  3. private static final String TAG = "MainActivity";
  4. MyRecycleAdapter recyclerAdapter;
  5. List<Pictures> imageList;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. imageList = new ArrayList<>();
  11. //Create RecycleView object and pin view
  12. RecyclerView recycleView = findViewById(R.id.linear_layout_with_recycleView_ID);
  13. GridLayoutManager linearLayoutManager = new GridLayoutManager(this, 2);
  14. //Setup Adapter
  15. recycleView.setLayoutManager(linearLayoutManager);
  16. recyclerAdapter = new MyRecycleAdapter(MainActivity.this, imageList);
  17. recycleView.setAdapter(recyclerAdapter);
  18. Log.d(TAG, "onCreate: ------>called<-----");
  19. //Creating reference for MyService and receiving deserialized data.
  20. ApiService apiClient = ApiClient.getClient().create(ApiService.class);
  21. Call<List<Pictures>> call = apiClient.getFile();
  22. call.enqueue(new Callback<List<Pictures>>() {
  23. @Override
  24. public void onResponse(Call<List<Pictures>> call, Response<List<Pictures>> response) {
  25. imageList = response.body();
  26. Log.d(TAG, "onResponse: ------>called<-----");
  27. recyclerAdapter.setMyRecycleAdapter(imageList);
  28. }
  29. @Override
  30. public void onFailure(Call<List<Pictures>> call, Throwable t) {
  31. Log.d("TAG", "onFailure = ------>called<----- " + t.toString());
  32. }
  33. });
  34. }
  35. }
英文:

I want to display images from https://picsum.photos/ into the imageView using recyclerView. Note that "https://picsum.photos/200" gets you a random square (200x200) image. I'm not sure if I'm using Glide and retrofit correctly (for arraylist with links it worked perfectly). I apriciate any hints. Thanks!
Edit: link to repo with all code: https://github.com/LightingTT/RecycleViewPictures

This is my ApiService:

  1. public interface ApiService {
  2. @GET(&quot;200/&quot;)
  3. Call&lt;List&lt;Pictures&gt;&gt; getFile();

</code>

ApiClient:

  1. public class ApiClient {
  2. public static String BASE_URL = &quot;https://picsum.photos/&quot;;
  3. private static Retrofit retrofit;
  4. public static Retrofit getClient(){
  5. if(retrofit == null){
  6. retrofit = new Retrofit.Builder()
  7. .baseUrl(BASE_URL)
  8. .addConverterFactory(GsonConverterFactory.create())
  9. .build();
  10. }
  11. return retrofit;
  12. }

Object class:

  1. public class Pictures {
  2. @SerializedName(&quot;picture&quot;)
  3. private String picturesUrl;
  4. public Pictures (String picturesUrl)
  5. {
  6. this.picturesUrl = picturesUrl;
  7. }
  8. public String getImageUrl() {
  9. return picturesUrl;
  10. }

Adapter:

  1. public class MyRecycleAdapter extends RecyclerView.Adapter&lt;MyRecycleAdapter.ViewHolderClass&gt; {
  2. private static final String TAG = &quot;MainActivity&quot;;
  3. private Context context;
  4. private List&lt;Pictures&gt; imageList;
  5. //Constructor
  6. public MyRecycleAdapter(Context context, List&lt;Pictures&gt; imageList)
  7. {
  8. this.context = context;
  9. this.imageList = imageList;
  10. }
  11. //
  12. public void setMyRecycleAdapter(List&lt;Pictures&gt; imageList)
  13. {
  14. this.imageList = imageList;
  15. notifyDataSetChanged();
  16. }
  17. @NonNull
  18. @Override
  19. public ViewHolderClass onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  20. View view = LayoutInflater.from(context).inflate(R.layout.single_picture_view, parent, false);
  21. ViewHolderClass linearViewHolderClass = new ViewHolderClass(view);
  22. return linearViewHolderClass;
  23. }
  24. @Override
  25. public void onBindViewHolder(@NonNull ViewHolderClass holder, int position) {
  26. Glide
  27. .with(context)
  28. .load(imageList.get(position).getImageUrl())
  29. .into(holder.imageView);
  30. Log.d(TAG, &quot;onBindViewHolder: ------&gt;called&lt;-----&quot;);
  31. }
  32. @Override
  33. public int getItemCount() {
  34. return imageList.size();
  35. }
  36. public class ViewHolderClass extends RecyclerView.ViewHolder{
  37. private ImageView imageView;
  38. //Constructor
  39. public ViewHolderClass(@NonNull View itemView) {
  40. super(itemView);
  41. imageView = itemView.findViewById(R.id.single_picture_id);
  42. }
  43. }

And MainActivity:

  1. public class MainActivity extends AppCompatActivity {
  2. private static final String TAG = &quot;MainActivity&quot;;
  3. MyRecycleAdapter recyclerAdapter;
  4. List&lt;Pictures&gt; imageList;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. imageList = new ArrayList&lt;&gt;();
  10. //Create RecycleView object and pin view
  11. RecyclerView recycleView = findViewById(R.id.linear_layout_with_recycleView_ID);
  12. GridLayoutManager linearLayoutManager = new GridLayoutManager (this, 2);
  13. //Setup Adapter
  14. recycleView.setLayoutManager(linearLayoutManager);
  15. recyclerAdapter = new MyRecycleAdapter(MainActivity.this, imageList);
  16. recycleView.setAdapter(recyclerAdapter);
  17. Log.d(TAG, &quot;onCreate: ------&gt;called&lt;-----&quot;);
  18. //Creating reference for MyService and receiving deserialized data.
  19. ApiService apiClient = ApiClient.getClient().create(ApiService.class);
  20. Call&lt;List&lt;Pictures&gt;&gt; call = apiClient.getFile();
  21. call.enqueue(new Callback&lt;List&lt;Pictures&gt;&gt;() {
  22. @Override
  23. public void onResponse(Call&lt;List&lt;Pictures&gt;&gt; call, Response&lt;List&lt;Pictures&gt;&gt; response) {
  24. imageList = response.body();
  25. Log.d(TAG, &quot;onResponse: ------&gt;called&lt;-----&quot;);
  26. recyclerAdapter.setMyRecycleAdapter(imageList);
  27. }
  28. @Override
  29. public void onFailure(Call&lt;List&lt;Pictures&gt;&gt; call, Throwable t) {
  30. Log.d(&quot;TAG&quot;,&quot;onFailure = ------&gt;called&lt;----- &quot;+t.toString());
  31. }
  32. });
  33. }

}

答案1

得分: 0

看起来我当时调用了错误的 API。应该是这样的:

  1. public interface ApiService {
  2. @GET("/v2/list")
  3. Call<List<Pictures>> getFile();
  4. }

而且对象类也有问题。由 http://www.jsonschema2pojo.org/ 生成:

  1. public class Pictures {
  2. @SerializedName("id")
  3. @Expose
  4. private String id;
  5. @SerializedName("author")
  6. @Expose
  7. private String author;
  8. @SerializedName("width")
  9. @Expose
  10. private Integer width;
  11. @SerializedName("height")
  12. @Expose
  13. private Integer height;
  14. @SerializedName("url")
  15. @Expose
  16. private String url;
  17. @SerializedName("download_url")
  18. @Expose
  19. private String downloadUrl;
  20. public String getId() {
  21. return id;
  22. }
  23. public void setId(String id) {
  24. this.id = id;
  25. }
  26. public String getAuthor() {
  27. return author;
  28. }
  29. public void setAuthor(String author) {
  30. this.author = author;
  31. }
  32. public Integer getWidth() {
  33. return width;
  34. }
  35. public void setWidth(Integer width) {
  36. this.width = width;
  37. }
  38. public Integer getHeight() {
  39. return height;
  40. }
  41. public void setHeight(Integer height) {
  42. this.height = height;
  43. }
  44. public String getUrl() {
  45. return url;
  46. }
  47. public void setUrl(String url) {
  48. this.url = url;
  49. }
  50. public String getDownloadUrl() {
  51. return downloadUrl;
  52. }
  53. public void setDownloadUrl(String downloadUrl) {
  54. this.downloadUrl = downloadUrl;
  55. }
  56. }

现在它能够正常工作,并在网格视图中显示出漂亮的随机图片。

英文:

It seems that I was asking wrong api. It should be

  1. public interface ApiService {
  2. @GET(&quot;/v2/list&quot;)
  3. Call&lt;List&lt;Pictures&gt;&gt; getFile();

}

And object class was wrong. Generated by http://www.jsonschema2pojo.org/

  1. public class Pictures {
  2. @SerializedName(&quot;id&quot;)
  3. @Expose
  4. private String id;
  5. @SerializedName(&quot;author&quot;)
  6. @Expose
  7. private String author;
  8. @SerializedName(&quot;width&quot;)
  9. @Expose
  10. private Integer width;
  11. @SerializedName(&quot;height&quot;)
  12. @Expose
  13. private Integer height;
  14. @SerializedName(&quot;url&quot;)
  15. @Expose
  16. private String url;
  17. @SerializedName(&quot;download_url&quot;)
  18. @Expose
  19. private String downloadUrl;
  20. public String getId() {
  21. return id;
  22. }
  23. public void setId(String id) {
  24. this.id = id;
  25. }
  26. public String getAuthor() {
  27. return author;
  28. }
  29. public void setAuthor(String author) {
  30. this.author = author;
  31. }
  32. public Integer getWidth() {
  33. return width;
  34. }
  35. public void setWidth(Integer width) {
  36. this.width = width;
  37. }
  38. public Integer getHeight() {
  39. return height;
  40. }
  41. public void setHeight(Integer height) {
  42. this.height = height;
  43. }
  44. public String getUrl() {
  45. return url;
  46. }
  47. public void setUrl(String url) {
  48. this.url = url;
  49. }
  50. public String getDownloadUrl() {
  51. return downloadUrl;
  52. }
  53. public void setDownloadUrl(String downloadUrl) {
  54. this.downloadUrl = downloadUrl;
  55. }

}

Now it works and displays nice random pictures in gridview.

huangapple
  • 本文由 发表于 2020年10月16日 05:11:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64379724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定