感冒没好
2016-12-05 12:07
采纳率: 50%
浏览 3.5k

Retrofit2 + Rxjava 请求网络图片 的问题

接口如下
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数据,是因为这个原因吗?
            现在一头雾水,问题出现在哪里?
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 感冒没好 2016-12-10 07:12
    已采纳

    直接去掉的话会报错
    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;
    }
    };
    }
    })

    还有简单的方法吗?

    已采纳该答案
    打赏 评论
  • devmiao 2016-12-05 16:27
    打赏 评论
  • bunny小巴 2016-12-06 01:57

    你加了这个GsonConverterFactory.create(),你的程序肯定会以json形式解析返回的结果,把这个去掉就OK了

    打赏 评论

相关推荐 更多相似问题