2017-02-07 3 views
0

Ich verwende Retrofit, um Dateien auf einen Server hochzuladen. Es funktioniert manchmal richtig, aber manchmal (und ziemlich oft), gibt es mir Antwort 400 (schlechte Anfrage) auf die gleiche Anfrage. Ist es ein bekanntes Problem oder was könnte das Problem sein?Android Retrofit 2: Zufälliger Code 400 (schlechte Anfrage) Antwort

Dieser gesamte Code für den Upload:

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

}

EDIT:

Dies ist die errorBody Nachricht:

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

Haben Sie überprüft, in welchem ​​Fall der Fehler auftritt? – dipali

+0

Es gibt keine Regel. Es passiert einfach. Manchmal fängt es nach 4-5 Versuchen an zu arbeiten, manchmal muss ich die App neu starten und es wird immer noch nicht funktionieren, manchmal wird es funktionieren. – Sermilion

+0

wird es auf jeden Fall Erfolg Antwort? – dipali

Antwort

1

So fixiert ich diese meine reinitializing OkHttpClient jeder ew Anfrage. Wahrscheinlich hat es einige alte Daten darin. Wenn irgendjemand das näher ausführen könnte, wäre das großartig.

+0

versuchen Sie diese Regel in Ihrer Kopfzeile: "Cache-Control: No-Cache" –