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

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;
    }
    };
    }
    })

    还有简单的方法吗?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作