下载带有JSON数据的图片(retrofit2 android)

时间:2019-06-25 13:09:59

标签: java android retrofit retrofit2

我要下载图像,但url图像不是直接的,应该发送类似的其他数据:

url : http://example.com/download

数据:

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

和服务器回调数据映像,格式为base64

我使用此代码,但服务器返回500个代码:

restservice:

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

Java代码:

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();
                    }
                }


            });

1 个答案:

答案 0 :(得分:0)

这是一个小示例,显示了如何下载Retrofit JAR文件。您可以根据需要进行调整。 这是界面:

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);
}

这是使用接口的Java类:

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) {
            }