Server provides the API to load image through Post request, whose body looks like
{
"image_id": someId,
"session_id": "someId"
}
Response - stream. How to download an image using Glide via HTTP POST method?
Server provides the API to load image through Post request, whose body looks like
{
"image_id": someId,
"session_id": "someId"
}
Response - stream. First you have to create a Model Object for the above response
Example :
import android.os.Parcel;
import android.os.Parcelable;
public class Movies implements Parcelable {
private String poster_path;
private String id;
public Movies() {
}
private Movies(Parcel in) {
poster_path = in.readString();
id = in.readString();
}
public static final Creator<Movies> CREATOR = new Creator<Movies>() {
@Override
public Movies createFromParcel(Parcel in) {
return new Movies(in);
}
@Override
public Movies[] newArray(int size) {
return new Movies[size];
}
};
public String getPoster_path() {
return poster_path;
}
public void setPoster_path(String poster_path) {
this.poster_path = poster_path;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(poster_path);
dest.writeString(id);
}
}
Once Model is create from the response we need to save those data.
private void fetchTask(final String movieType) {
if (jsonObjectRequest != null) {
jsonObjectRequest.cancel();
movies.clear();
}
Uri.Builder builder = new Uri.Builder();
builder.scheme("https")
.authority("api.themoviedb.org")
.appendPath("3")
.appendPath("movie")
.appendPath(movieType)
.appendQueryParameter(MOVIE_API_KEY, MOVIE_API_VALUE);
String url = builder.build().toString();
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray(MovieModelConstants.MOVIE_RESULTS);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Movies data = new Movies();
data.setPoster_path(jsonObject.getString(MOVIE_POSTER_PATH));
data.setId(jsonObject.getString(MOVIE_ID));
movies.add(data);
}
moviesAdapter = new MoviesAdapter(movies);
moviesAdapter.setItemClickListener(clickListener);
recyclerViewMovies.setAdapter(moviesAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), getString(R.string.errorMessage), Toast.LENGTH_SHORT);
}
});
Volley.newRequestQueue(getContext()).add(jsonObjectRequest);
}
}
In the Adapter you need load based on the position
Picasso.with(holder.mMovieItem.getContext()).load(generateUrlForPoster(String.valueOf(data.get(position).getPoster_path()))).into(holder.mMovieItem);
Algorithm for loading images using Glide & Retrofit 2 via HTTP POST method:
1) Create interface, which should be implemented by all requests for image loading via HTTP POST method:
2) Create model to load, which will be used as a parameter for Glide.with(context).load(model):
3) Create an implementation of com.bumptech.glide.load.data.DataFetcher:
4) Create an implementation of com.bumptech.glide.load.model.ModelLoader:
5) Create an implementation of GlideModule, which allows configure Glide
6) Add meta data about created RetrofitGlideModule in AndroidManifest.xml
Now you can download images next way: