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

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 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!