2016-06-03 5 views
0

Ich versuche, meine Codebasis von Retrofit 1.9 auf Version 2.0 zu aktualisieren. Ich habe kein Problem beim Hochladen eines Bildes in Version 1.9. Jedoch bekomme ich folgenden S3-Antwortfehler, nachdem ich es in Version 2.0 geändert habe.Wie stelle ich Retrofit2, um meine dynamische URL nicht zu codieren?

Diese meine Schnittstelle ist Bild in Retrofit v1.9 hochladen:

public interface IFileUploadAPI 
{ 
    @PUT 
    Call<UploadFileResponse> uploadFile(@Url String url, @Body RequestBody file); 
} 

Und ich bin der Umsetzung:

public interface IFileUploadAPI 
{ 
    @PUT("/{url}") 
    void uploadFile(@Path(value = "url", encode = false) String url, @Body() TypedFile file, 
        Callback<UploadFileResponse> callback); 
} 

Ich habe es zu folgenden Code auf Retrofit v2.0 geändert es auf diese Weise.

public class MyFileUploadAPI 
{ 
    private IFileUploadAPI mService; 

    public MyFileUploadAPI() 
    { 
     final Retrofit adapter = new Retrofit.Builder() // 
         .baseUrl(MyAPIConstant.API_URL_BASE) // This URL will be ignored as we are using dynamic url. So no matters what it is. 
         .addConverterFactory(GsonConverterFactory.create(GsonUtils.getGson())) // 
         .client(ASDKApplication.getInstance().getOkHttpClient()) // 
         .build(); 

     this.mService = adapter.create(IFileUploadAPI.class); 
    } 

    public void uploadFile(final String url, final String filePath, final String mimeType) 
    { 
     RequestBody file = RequestBody.create(MediaType.parse(mimeType), filePath); 
     Call<UploadFileResponse> call = mService.uploadFile(url, file); 
       call.enqueue(new Callback<HitchUploadFileResponse>() 
     { 
      @Override 
      public void onResponse(Call<UploadFileResponse> call, Response<UploadFileResponse> response) 
      { 
       UploadFileResponse uploadFileResponse = new UploadFileResponse(); 

       if (response.isSuccessful() && response.body() != null) 
       { 
        uploadFileResponse = response.body(); 
       } 

       // Rest of code... 
      } 

      @Override 
      public void onFailure(Call<UploadFileResponse> call, Throwable t) 
      { 
       Logger.error(TAG, "uploadFile.onFailure(), msg: " + t.getMessage()); 
      } 
     } 
    } 
} 

Also, wenn Benutzer klicken Sie auf Upload Taste, nenne ich eine API und es gibt mir eine URL, die ich anrufen. Die Antwort sieht Vorlieben:

[{"uploadSignedURL":"https://my-account.s3.amazonaws.com/license/1000_front_1464988248.jpeg?AWSAccessKeyId=MY_ID\u0026Expires=1464991848\u0026Signature=NhStdEX8RH3J0uzvVa6h%2FvN6FZQ%3D","filePath":"license/1000_front_1464988248.jpeg","target":"driving_license_front_img"}] 

Und schließlich die Antwort über URL (HTTP-Statuscode 403 verboten) nennen von:

<?xml version="1.0" encoding="UTF-8"?> 
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><AWSAccessKeyId>MY_ID</AWSAccessKeyId><StringToSign>PUT 

image/jpeg; charset=utf-8 
1464991848 
/my-account/license/1000_front_1464988248.jpeg</StringToSign><SignatureProvided>NhStdEX8RH3J0uzvVa6h/vN6FZQ=</SignatureProvided><StringToSignBytes>50 55 54 0a 0a 69 6d 61 67 65 2f 6a 70 65 67 3b 20 63 68 61 72 73 65 74 3d 75 74 66 2d 38 0b 31 34 36 34 39 39 31 38 34 38 0a 2f 6d 79 74 65 6b 73 69 3a 67 72 61 62 68 69 45 63 68 2f 6c 69 63 65 6e 73 65 2f 31 30 30 30 5f 66 72 6f 6e 74 5f 31 34 36 34 39 38 38 32 34 38 2e 6a 70 65 67</StringToSignBytes><RequestId>2D6D08FE4E34FCE5</RequestId><HostId>tpvmJdeRxtAzPxOk2zFwMF2Ne6hmNMkcaM9D7m/I13iynYGJyqtC0U72B2t41SDYfPBjCRTVyOY=</HostId></Error> 

So würde jede Idee dankbar. Vielen Dank.

Antwort

0

Ok, ich habe festgestellt, was los ist.

okhttp fügt charset=utf-8 in meine contentType. Daher wird mein contentType wie folgt contentType: image/jpeg; charset=utf-8. Ich habe auf Postman getestet und durch Löschen von ; charset=utf-8 kann ich mein Bild hochladen.

https://stackoverflow.com/a/25560731/513413 sagt den Grund des Hinzufügens charset. Es liegt daran, dass ich den Pfad zu meiner Bilddatei übergebe. Sie können ganz einfach

RequestBody file = RequestBody.create(MediaType.parse(mimeType), filePath);

zu

RequestBody file = RequestBody.create(MediaType.parse(mimeType), new File(filePath));

Es ändern noch eine wichtige Sache ist. Sobald Sie Ihr Bild in S3 hochgeladen haben, gibt Amazon den HTTP-Statuscode 200 mit leerem Text zurück. Sie müssen also auch die Schnittstelle ändern.

public interface IFileUploadAPI 
{ 
    @PUT 
    Call<Void> uploadFile(@Url String url, @Body RequestBody file); 
}