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

时间:2017-02-07 04:55:02

标签: android retrofit2 bad-request

我正在使用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>

1 个答案:

答案 0 :(得分:1)

所以我修复了每次ew请求我重新初始化的OkHttpClient。可能它里面有一些旧数据。如果有人能详细说明,那就太好了。