1

I want download image but url image is not direct and should send additional data like that :

url : http://example.com/download

data :

 {
    "phoneNumber":"9199191",
    "token":"1KAwqCxCdQUjTvTK9EtT7N",
    "fileName":"632_macbook_pro.jpg"
    }

and server callback data image In the form of base64

i use this codes but server return 500 code :

restservice :

@POST("download")
    Call<ResponseBody> getImage(
            @Body JsonObject data,
            @HeaderMap Map<String, String> headers
    );

Java code:

JsonObject params = new JsonObject();
            params.addProperty("phoneNumber",settingMain.getPhoneNum());
            params.addProperty("token",settingMain.getSecureLogin());
            params.addProperty("fileName",fileName);

            Call<ResponseBody> myCall = restService.getImage(params, UrlController.AddHeadersDownload(fileName));
            myCall.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> responseObj) {
                    try {
Log.e("DownloadCode",responseObj.code()+"");
                        if (responseObj.isSuccessful() && responseObj.code()==200) {

} catch (JSONException e) {
                        e.printStackTrace();
                        Log.e("JSONException",e.toString());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            });
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
AhmadReza
  • 421
  • 6
  • 20
  • if you are getting base64 code than you can convert to bitmap to show in imageView, for this link, https://stackoverflow.com/questions/4837110/how-to-convert-a-base64-string-into-a-bitmap-image-to-show-it-in-a-imageview can help out. – PJain Jun 25 '19 at 13:20

1 Answers1

0

This is a little example showing how to download the Retrofit JAR file. You can adapt it to your needs. This is the interface:

import com.squareup.okhttp.ResponseBody;
import retrofit.Call;
import retrofit.http.GET;
import retrofit.http.Path;

interface RetrofitDownload {
    @GET("/maven2/com/squareup/retrofit/retrofit/2.0.0-beta2/{fileName}")
    Call<ResponseBody> downloadRetrofit(@Path("fileName") String fileName);
}

And this is a Java class using the interface:

import com.google.common.io.Files;
import com.squareup.okhttp.ResponseBody;
import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;

import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String... args) {
        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://repo1.maven.org").
                build();

        RetrofitDownload retrofitDownload = retrofit.create(RetrofitDownload.class);

        Call<ResponseBody> call = retrofitDownload.downloadRetrofit("retrofit-2.0.0-beta2.jar");

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Response<ResponseBody> response, Retrofit retrofitParam) {
                File file = new File("retrofit-2.0.0-beta2.jar");
                try {
                    file.createNewFile();
                    Files.asByteSink(file).write(response.body().bytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable t) {
            }
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58
  • thanks for your answer but i need send token and phone number user for Checking user identity. and this data should send with POST method – AhmadReza Jun 25 '19 at 13:23