2016-04-12 10 views
2

Android携帯からサーバーに画像をアップロードするためにRetrofitバージョン2.0.1を使用しましたが、奇妙なエラーが発生しました。 これは、サーバーへのポストのための私のAPIインタフェースです:Retrofit 2:サーバーに画像をアップロードして奇妙な応答を得る

@POST("/vistek/myservice.php") 
    Call<String> send(@Part("myFile") RequestBody file); 

マイOkttpクライアントは、次のとおりです。

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
    HttpLoggingInterceptor interceptor2 = new HttpLoggingInterceptor(); 
    interceptor2.setLevel(HttpLoggingInterceptor.Level.HEADERS); 

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 

    httpClient.addInterceptor(interceptor); 
    httpClient.addInterceptor(interceptor2); 

、サーバに画像を送信するための私の機能:

public void sendImage(byte[] data) 
    { 
     MediaType MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg"); 
     FileUploadService service = ServiceGenerator.createService(FileUploadService.class); 

     RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JPEG, data); 

     Call<String> call = service.send(requestBody); 

     call.enqueue(new Callback<String>() { 
      @Override 
      public void onResponse(Call<String> call, retrofit2.Response<String> response) { 
       Log.d("Upload", "success = " + response.body()); 
      } 

      @Override 
      public void onFailure(Call<String> call, Throwable t) { 

       Log.d("Upload","failure: " + t.getMessage()); 
      } 
     }); 
    } 

コール機能した後、私は奇妙な反応を得ました。このようなもの:

JFIF C C "
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: } !1AQa "q2 #B R $3br
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp:%& '()* 456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz
04-12 17:30:11.144 11669-13785/mmlab.visualsearch D/OkHttp: w !1

POST /vistek/myservice.php HTTP/1.1 
Host: demo.mmlab.uit.edu.vn 
Cache-Control: no-cache 
Postman-Token: ba965fb1-f7b9-7344-8b8a-ab46095668d1 
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW 

----WebKitFormBoundary7MA4YWxkTrZu0gW 
Content-Disposition: form-data; name="myFile"; filename="" 
Content-Type: 

----WebKitFormBoundary7MA4YWxkTrZu0gW 

は私のコードで起こるかを知るために私を助けてください。私はポストマンでAPIをテストするときAQaq「2B#3Rbr

私のサーバが正常に動作します。 ありがとうございます。

答えて

1

奇妙な応答です。圧縮されています(gzip)。

"accept-encoding", "gzip"headerに追加したようです。その場合、回答を解凍するのはあなたの責任です。

それでは、あなたにできることは次のとおりです。

ちょうどあなたのコードからaccept-encodingヘッダーを省略します。 OkHttpは独自のaccept-encodingヘッダーを追加し、サーバーがgzipで応答すると、OkHttpは静かに解凍します。

また、手動でこのようにそれを解凍することができます

StringBuilder stringBuilder = new StringBuilder(); 
ByteArrayInputStream bais = new ByteArrayInputStream(response.body().getBytes()); 
GZIPInputStream gzis = new GZIPInputStream(bais); 
InputStreamReader reader = new InputStreamReader(gzis); 
BufferedReader in = new BufferedReader(reader); 

String readed; 
while ((readed = in.readLine()) != null) { 
    stringBuilder.append(readed); 
} 
in.close(); 
reader.close(); 
gzis.close(); 
String unZippedResponse = stringBuilder.toString(); 
+0

私の要求は、ヘッダ –

+0

で「受け入れエンコード」を、「GZIP」が含まれていないあなたは、あなたが 'OkHttp'クライアントを作成する方法を投稿していることができます任意のインターセプタを使用していますか? –

+0

私のOkHttpクライアントは: HttpLoggingInterceptorインターセプタ2 =新しいHttpLoggingInterceptor(); interceptor2.setLevel(HttpLoggingInterceptor.Level.HEADERS); HttpLoggingInterceptorインターセプタ=新しいHttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); httpClient.addInterceptor(インターセプタ); httpClient.addInterceptor(interceptor2); –

関連する問題