2017-05-27 88 views
0

我正在使用Retrofit 2來調用一個微服務,它返回一個200和一個PUT方法上的空響應主體。 改造2但似乎不能夠處理這一點,並在「排隊」進入到onFailure處分支
@覆蓋 公共無效onFailure處(電話呼叫,的Throwable T){Retrofit2 OkHttp3響應正文空錯誤

下面是日誌:

Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log 
INFORMATION: --> PUT http://127.0.0.1/test/ http/1.1 
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log 
INFORMATION: Content-Type: application/vnd.tipico.notification-v1+json 
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log 
INFORMATION: Content-Length: 87 
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log 
INFORMATION: 
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log 
INFORMATION: {"state":"ACTIVE","externalId":"abcd","loginName":"gsdfgsdf","updatedAt":1495531062000} 
Mai 27, 2017 3:26:49 PM okhttp3.internal.platform.Platform log 
INFORMATION: --> END PUT (87-byte body) 
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log 
INFORMATION: <-- 200 http://127.0.0.1/test/ (197ms) 
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log 
INFORMATION: X-Application-Context: customer-care-notification-service:49980 
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log 
INFORMATION: Content-Length: 0 
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log 
INFORMATION: Date: Sat, 27 May 2017 13:26:49 GMT 
Mai 27, 2017 3:26:50 PM okhttp3.internal.platform.Platform log 
INFORMATION: <-- END HTTP (0-byte body) 
15:26:50,030 ERROR com.test.app.Test - Failed CCNS call com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input 
at [Source: [email protected]; line: 1, column: 0] 

任何人都知道是什麼原因造成的?由於請求已成功發送(請參見上文)。

+0

這是相當奇怪之前添加該轉換器。您的電話是否會返回任何可能會嘗試進行反序列化的對象?通過電話我的意思是java界面 – Fred

+0

你找到解決方案嗎?我面臨同樣的問題。看起來這是一些傑克遜版本發生。你正在使用哪個版本? – Pawan

+0

嗨,是的,你需要像這裏https://github.com/square/retrofit/issues/1554 – razvan

回答

0

我創建了一個空的處理程序轉換器針對此問題:

public class NullOnEmptyConverterFactory extends Converter.Factory { 

    @Override 
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { 
     final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter(this, type, annotations); 
     return (Converter<ResponseBody, Object>) body -> { 
      if (body.contentLength() == 0) return null; 
      return delegate.convert(body); 
     }; 
    } 
} 

你需要你的GSON轉換器

.addConverterFactory(new NullOnEmptyConverterFactory())