retrofit2.0转换String 报错修复
2015-11-19 14:36
323 查看
添加Gson转换支持
然后这样设置后 Call< String>会出现gson类型转换异常
需要添加字符串转换
最后添加
new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .baseUrl(AppVars.RequestPath.BASE_URL) .client(App.getOkHttpClient()) .build();
然后这样设置后 Call< String>会出现gson类型转换异常
需要添加字符串转换
import com.squareup.okhttp.ResponseBody; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.lang.annotation.Annotation; import retrofit.Converter; /** * retrofit转换成字符串支持 * Created by Administrator on 2015/11/19. */ public final class StringConverterFactory extends Converter.Factory { public static StringConverterFactory create() { return new StringConverterFactory(); } @Override public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) { return new ConfigurationServiceConverter(); } final class ConfigurationServiceConverter implements Converter<ResponseBody, String> { @Override public String convert(ResponseBody value) throws IOException { BufferedReader r = new BufferedReader(new InputStreamReader(value.byteStream())); StringBuilder total = new StringBuilder(); String line; while ((line = r.readLine()) != null) { total.append(line); } return total.toString(); } } }
最后添加
retrofit = new Retrofit.Builder().addConverterFactory(StringConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .baseUrl(AppVars.RequestPath.BASE_URL) .client(App.getOkHttpClient()) .build();
相关文章推荐
- 看到C上这么多努力的大神有感
- swif自学笔记(二)
- [Leetcode]Search a 2D Matrix II
- block的简单回调传值
- Servlet:JSP理解
- linker command failed with exit code 1
- About ANR and OOM
- Asp.Net HttpApplication请求管道与Session(二)
- iOS中 XMPP即时通讯实现的主要步骤
- php保存base64数据
- android四大组件之Service 播放音乐
- web性能优化
- 连续ping IP地址 shell脚本【来自《鸟哥linux的私房菜》】
- 祝兄弟们旗开得胜—兄弟连IT教育
- 【Android】长按连续触发事件的实现方法
- Android编程实现仿心跳动画效果的方法
- Node初学者入门,一本全面的NodeJS教程(推荐阅读)
- Android实现启动页停留几秒然后自动跳转到App首页的方法
- android录制声音写入文件
- java获取访问者的IP