2016-04-12 18 views
0

I (Henüz 2.0.x için hareket edemez)Uyarlama 1.9.x

Ben This Post okudum Retrofitler 1.9.x kullanarak bir FileInputStream POST çalışıyorum bir FileInputStream deftere nakil.

Bu yazımda, arayüzümde bir TypedInput kullanırsam ve akışı işleyen TypedInput sınıf sarıcısını kullanırsam, bunun işe yaradığını anladım. TypedInput ve TypedOutput'ın yanıtı (TypedInput en doğru ses çıktıysa, bağdaştırılmış retrofit belgelerinin söyleyebildiğim kadarını belirtmediyse de açık değil) Ayrıca,

Devam etmek için - sınıf

private class InputStreamMunger implements TypedInput { 
    private InputStream is; 
    private String mimeType; 
    private Long fileLength; 

    public InputStreamMunger(InputStream is, String mimeType, Long fileLength) { 
     this.is = is; 
     this.fileLength = fileLength; 
     this.mimeType = mimeType; 
    } 

    @Override 
    public String mimeType() { 
     return mimeType; 
    } 

    @Override 
    public long length() { 
     return fileLength; 
    } 

    @Override 
    public InputStream in() throws IOException { 
     return is; 
    } 
} 

Benim Arayüz:

@Multipart 
@POST("/MrService/v1/upload/{accountId}") 
Response upload(
    @Path("accountId") String accountId, 
    @Part("file") TypedInput file); 

Sonra

FileInputStream is = new FileInputStream("src/test/java/com/me/MrService/tester.txt"); 
    InputStreamMunger file ; 

    try { 
     file = new InputStreamMunger(is, "text/plain", is.getChannel().size()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return; 
    } 

    Response r = client.upload("12345", file); 
diyoruz 363.210

i almak hatadır:

retrofit.RetrofitError: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.me.MrService.IntegrationTestIT$InputStreamMunger and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)) 

bu bir Sohbet işlemek için kendi mapper oluşturmak gerektiğini ifade ediyor mu? Umarım sadece yanlış bir şey yapıyorum ve bu kasnağa atlamaya ihtiyacım yok.

Teşekkürler!

cevap

0

Sonuçta, TypedInput yerine TypedOutput uyguladım.

private class InputStreamMunger implements TypedOutput { 

    private InputStream is; 
    private String mimeType; 
    private Long fileLength; 
    private String fileName; 
    private static final int BUFFER_SIZE = 4096; 


    public InputStreamMunger(InputStream is, String mimeType, Long fileLength, 
      String fileName) { 
     this.is = is; 
     this.fileLength = fileLength; 
     this.mimeType = mimeType; 
     this.fileName = fileName; 
    } 

    @Override 
    public String mimeType() { 
     return mimeType; 
    } 

    @Override 
    public long length() { 
     return fileLength; 
    } 

    @Override 
    public void writeTo(OutputStream out) throws IOException { 
     byte[] buffer = new byte[BUFFER_SIZE]; 
     try { 
      int read; 
      while ((read = is.read(buffer)) != -1) { 
       out.write(buffer, 0, read); 
      } 
     } finally { 
      is.close(); 
     } 
    } 

    public String fileName() { 
     return fileName; 
    } 

} 
İlgili konular