Android Retrofit 2:随机代码400(错误请求)响应

塞米利翁

我正在使用Retrofit将文件上传到服务器。有时它可以正常工作,但是偶尔(并且经常),它给我对相同请求的响应400(错误请求)。这是一个已知问题还是可能是什么问题?

这是完整的上传代码:

public static void uploadFile(String authString,
                              Uri fileUri,
                              Context context,
                              MVPCallback<ResponseBody> mvpCallback) {
    FileUploadService service =
            ServiceGenerator.createService(FileUploadService.class, authString);
    String filePath = FacadeMedia.getPath(context, fileUri);
    if (filePath != null) {
        File uploadFile = new File(filePath);
        RequestBody requestFile = RequestBody.create(
                        MediaType.parse(context.getContentResolver().getType(fileUri)),
                        uploadFile);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", uploadFile.getName(), requestFile);
        String content_disposition = "file; filename=\"" + uploadFile.getName() + "\"";
        Call<ResponseBody> call = service.upload(body, content_disposition);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                if(response.isSuccessful()){
                    mvpCallback.onSuccess(response.body());
                }else {
                    mvpCallback.onError(new Throwable(response.errorBody().toString()));
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                mvpCallback.onError(t);
            }
        });
    }
}


public interface FileUploadService {

@Multipart
@POST(HttpFactory.UPLOAD_FILE_URL)
Call<ResponseBody> upload(
        @Part MultipartBody.Part file,
        @Header("Content-Disposition") String content_disposition
);
}


public class ServiceGenerator {

private static Retrofit retrofit;

private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

private static OkHttpClient.Builder httpClient =
        new OkHttpClient.Builder();

public static <S> S createService(Class<S> serviceClass, String authString)     {
    httpClient.addInterceptor(chain -> {
        Request request = chain.request()
                .newBuilder()
                .addHeader("Authorization", " Basic "+authString).build();
        return chain.proceed(request);
    });
    retrofit = builder.client(httpClient.build()).build();
    return retrofit.create(serviceClass);
}

}

编辑:

这是errorBody消息:

E/Bad request:: <html>
            <head><title>400 Bad Request</title></head>
            <body bgcolor="white">
            <center><h1>400 Bad Request</h1></center>
            <hr><center>cloudflare-nginx</center>
            </body>
            </html>
塞米利翁

因此,我解决了这个问题,因为每个ew请求都需要重新初始化OkHttpClient。可能其中包含一些旧数据。如果任何人都可以对此进行详细说明,那就太好了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章