简体   繁体   中英

Download an image using Retrofit and Glide

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:

public interface ApiService {

@GET("200/")
Call<List<Pictures>> getFile();

ApiClient:

public class ApiClient {
public static String BASE_URL = "https://picsum.photos/";
private static Retrofit retrofit;
public static Retrofit getClient(){
if(retrofit == null){
    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}
    return retrofit;
}

Object class:

public class Pictures {

@SerializedName("picture")
private String picturesUrl;

public Pictures (String picturesUrl)
{
    this.picturesUrl = picturesUrl;
}

public String getImageUrl() {
    return picturesUrl;
}

Adapter:

public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.ViewHolderClass> {

private static final String TAG = "MainActivity";
private Context context;
private List<Pictures> imageList;

//Constructor
public MyRecycleAdapter(Context context, List<Pictures> imageList)
{
    this.context = context;
    this.imageList = imageList;
}
//
public void setMyRecycleAdapter(List<Pictures> imageList)
{
    this.imageList = imageList;
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolderClass onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.single_picture_view, parent, false);
    ViewHolderClass linearViewHolderClass = new ViewHolderClass(view);
    return linearViewHolderClass;
}

@Override
public void onBindViewHolder(@NonNull ViewHolderClass holder, int position) {
    Glide
                    .with(context)
                    .load(imageList.get(position).getImageUrl())
                    .into(holder.imageView);
    Log.d(TAG, "onBindViewHolder: ------>called<-----");
}

@Override
public int getItemCount() {
    return imageList.size();
}

public class ViewHolderClass extends RecyclerView.ViewHolder{

    private ImageView imageView;

    //Constructor
    public ViewHolderClass(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.single_picture_id);
    }
}

And MainActivity:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
MyRecycleAdapter recyclerAdapter;
List<Pictures> imageList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageList = new ArrayList<>();

    //Create RecycleView object and pin view
    RecyclerView recycleView = findViewById(R.id.linear_layout_with_recycleView_ID);
    GridLayoutManager linearLayoutManager = new GridLayoutManager (this, 2);

    //Setup Adapter
    recycleView.setLayoutManager(linearLayoutManager);
    recyclerAdapter = new MyRecycleAdapter(MainActivity.this, imageList);
    recycleView.setAdapter(recyclerAdapter);

    Log.d(TAG, "onCreate: ------>called<-----");
    
    //Creating reference for MyService and receiving deserialized data.
    ApiService apiClient = ApiClient.getClient().create(ApiService.class);
    Call<List<Pictures>> call = apiClient.getFile();

    call.enqueue(new Callback<List<Pictures>>() {
        @Override
        public void onResponse(Call<List<Pictures>> call, Response<List<Pictures>> response) {
            imageList = response.body();
            Log.d(TAG, "onResponse: ------>called<-----");
            recyclerAdapter.setMyRecycleAdapter(imageList);
        }

        @Override
        public void onFailure(Call<List<Pictures>> call, Throwable t) {
            Log.d("TAG","onFailure = ------>called<----- "+t.toString());
        }
    });

}

}

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

public interface ApiService {

@GET("/v2/list")
Call<List<Pictures>> getFile();

}

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

public class Pictures {

@SerializedName("id")
@Expose
private String id;

@SerializedName("author")
@Expose
private String author;

@SerializedName("width")
@Expose
private Integer width;

@SerializedName("height")
@Expose
private Integer height;

@SerializedName("url")
@Expose
private String url;

@SerializedName("download_url")
@Expose
private String downloadUrl;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public Integer getWidth() {
    return width;
}

public void setWidth(Integer width) {
    this.width = width;
}

public Integer getHeight() {
    return height;
}

public void setHeight(Integer height) {
    this.height = height;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getDownloadUrl() {
    return downloadUrl;
}

public void setDownloadUrl(String downloadUrl) {
    this.downloadUrl = downloadUrl;
}

}

Now it works and displays nice random pictures in gridview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM