Pradeep CR Pradeep CR -5 years ago 560
Android Question

Retrofit API to retrieve a png image

Hi I am new to Retrofit framework for Android. I could get JSON responses from REST services using it but I don't know how to download a png using retrofit. I am trying to download the png from this url:
What should be response Object to be specified in the Callback<> to achieve this.

Answer Source

As mentioned you shouldn't use Retrofit to actually download the image itself. If your goal is to simply download the content without displaying it then you could simply use an Http client like OkHttp which is another one of Square's libraries.

Here's a few lines of code which would have you download this image. You could then read the data from the InputStream.

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()

    client.newCall(request).enqueue(new Callback() {
        public void onFailure(Request request, IOException e) {
            System.out.println("request failed: " + e.getMessage());

        public void onResponse(Response response) throws IOException {
            response.body().byteStream(); // Read the data from the stream

Even though Retrofit isn't the man for the job to answer your question, the signature of your Interface definition would like this. But again don't do this.

public interface Api {
    void getImage(@Path("imageName") String imageName, Callback<Response> callback);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download