Android-使用Retrofit 2上传照片和字符串

时间:2019-06-21 09:22:54

标签: android retrofit2

我正在尝试使用Retrofit 2将照片和令牌上传到服务器,但是未调用“ call”方法中的回调。

尝试了几种变体,但没有一种适合我。 这是我的最后一个配置:

链接:https://some_domain.com/cabinet/profile/upload-docs/address

接口:

public interface UploadApis {

@Multipart
@POST("address")
Call<ResponseBody> uploadAddress(
        @Part("description") RequestBody description,
        @Part MultipartBody.Part photo
        );
}

改造实例类:

public class UploadImageClient {

private static final String BASE_URL = "https://some_domain.com/cabinet/profile/upload-docs/";
private static Retrofit retrofit;

public static Retrofit getRetrofitClient(Context context) {
    if (retrofit == null) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

上传功能:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == this.RESULT_CANCELED) {
        return;
    }

    if (data != null) {
        UploadApis uploadApis = UploadImageClient.getRetrofitClient(MainActivity.this).create(UploadApis.class);

        Uri contentURI = data.getData();

        File originalFile = new File(contentURI.getPath());

        RequestBody descriptionPart = RequestBody.create(MultipartBody.FORM, InAppProperties.getInstance().tokenIsDemoPair.token);

        RequestBody filePart = RequestBody.create(MediaType.parse(getContentResolver().getType(contentURI)), originalFile);

        MultipartBody.Part file = MultipartBody.Part.createFormData("photo", originalFile.getName(), filePart);

        Call<ResponseBody> call = uploadApis.uploadAddress(descriptionPart, file);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                Log.d("Upload", "success");
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.d("Upload error:", t.getMessage());
            }
        });
    }
}

编辑: 等待6分钟后,我得到了异常:

okhttp3.internal.http2.ConnectionShutdownException

1 个答案:

答案 0 :(得分:0)

尝试使用OkHttpClient扩展超时限制。

  OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.MINUTES)
                .writeTimeout(10, TimeUnit.MINUTES)
                .readTimeout(30, TimeUnit.MINUTES)
                .build();

如果仍然无法获得确切的错误消息,则启用改造日志,这将打印网络的所有日志。

  HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

所以最后一个client看起来像

  OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.MINUTES)
                .writeTimeout(10, TimeUnit.MINUTES)
                .readTimeout(30, TimeUnit.MINUTES)
                .addInterceptor(logging)
                .build();
相关问题