Android 改造 2:随机代码 400(错误请求)响应
Android Retrofit 2: Random code 400 (bad request) response
我正在使用 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);
}
}
编辑:
这是错误正文消息:
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>
所以我修复了这个问题,我在每次新请求时都重新初始化 OkHttpClient。可能里面有一些旧数据。如果有人能对此进行详细说明,那就太好了。
我正在使用 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);
}
}
编辑:
这是错误正文消息:
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>
所以我修复了这个问题,我在每次新请求时都重新初始化 OkHttpClient。可能里面有一些旧数据。如果有人能对此进行详细说明,那就太好了。