接口如下
public interface ImgService {
@GET
Observable getImg(@Url String url);
}
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://pic41.nipic.com/")
.build();
ImgService imgService = retrofit.create(ImgService.class);
imgService.getImg("http://pic41.nipic.com/20140509/4746986_145156378323_2.jpg")
.subscribeOn(Schedulers.io())
.map(new Func1() {
@Override
public Bitmap call(ResponseBody responseBody) {
// 代碼還從來沒運行到過這個函數。。。
// responseBody.byteStream();
// return null;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(Bitmap bitmap) {
img.setImageBitmap(bitmap);
}
});
現在出現的異常是:
com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
這個圖片網址打開就是一張圖片,不是Json數據,是因為這個原因嗎?
現在一頭霧水,問題出現在哪裡?
直接去掉的話會報錯
E/AndroidRuntime: FATAL EXCEPTION: main
Process: hsy.com.httpdemo, PID: 3272
java.lang.RuntimeException: Unable to start activity ComponentInfo{hsy.com.httpdemo/hsy.com.httpdemo.MainActivity}: java.lang.IllegalArgumentException: Unable to create converter for class com.squareup.okhttp.ResponseBody
for method ImgService.getImg
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Unable to create converter for class com.squareup.okhttp.ResponseBody
for method ImgService.getImg
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:706)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:167)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at $Proxy0.getImg(Native Method)
at hsy.com.httpdemo.MainActivity.getImgByRxJavaAndRetrofit(MainActivity.java:246)
at hsy.com.httpdemo.MainActivity.onCreate(MainActivity.java:206)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
... 11 more
Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for class com.squareup.okhttp.ResponseBody.
Tried:
* retrofit2.BuiltInConverters
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:346)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:308)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:704)
... 20 more
不知道是因為我依賴加錯了還是啥 - -,錯誤好像說的是okhttp3的ResponseBody對象和okhttp2的ResponseBody對象轉換失敗。
所以需要自定義Converter
.addConverterFactory(new Converter.Factory() {
@Override
public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter() {
@Override
public ResponseBody convert(okhttp3.ResponseBody value) throws IOException {
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
com.squareup.okhttp.ResponseBody responseBody = com.squareup.okhttp.ResponseBody
.create(com.squareup.okhttp.MediaType.parse(mimeTypeMap.getMimeTypeFromExtension("jpg"))
,value.bytes());
return responseBody;
}
};
}
})
還有簡單的方法嗎?