升级okhttp3.0+和retrofit2.0+的过程以及遇到的坑
来源:互联网 发布:it发展规划 编辑:程序博客网 时间:2024/05/29 17:46
包名变化
okhttp3.0之前是:com.squareup.okhttp.*,
而到3.0之后变成:okhttp3.*
retrofit2.0之前:retrofit.*
retrofit2.0之后:retrofit2.*
都在末尾增加了版本号,显示的更专业,但是这也给我们升级带来了麻烦。
首先看一下retrofit1.9+okhttp2.2的配置代码
public class MainRetrofit { final MeoHttp mService; final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").serializeNulls().create(); RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("User-Agent", AppConstants.USER_AGENT); request.addHeader("X-Requested-With", "XMLHttpRequest"); request.addHeader("Cookie", AppConstants.PHPSESSID); } }; MainRetrofit() { OkHttpClient client = new OkHttpClient(); client.setReadTimeout(41, TimeUnit.SECONDS); RestAdapter restAdapter = new RestAdapter.Builder() .setClient(new OkClient(client)) .setEndpoint(MainFactory.HOST) .setConverter(new GsonConverter(gson)) .setLogLevel(RestAdapter.LogLevel.NONE) .setRequestInterceptor(requestInterceptor) .build(); mService = restAdapter.create(MeoHttp.class); } public MeoHttp getService(){ return mService; }}okhttp3.0以及retrofit2.0均采用了builder构建者模式配置
final RequestApi api; final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").serializeNulls().create(); Interceptor requestInterceptor; private static final okhttp3.OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); public MainRetrofit(final boolean hasCookie) { httpClient.readTimeout(60, TimeUnit.SECONDS). connectTimeout(2, TimeUnit.SECONDS).build(); requestInterceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); String cookie = ""; if (hasCookie) { cookie = AppConfig.Net.COOKIE_HEAD + CookieManager.getCookie(); } Request compressedRequest = request.newBuilder() .header("Cookie", cookie) .header("Accept-Language", Locale.getDefault().toString()) .header("Accept-Charset", "utf-8") .header("Connection", "Keep-Alive") .header("User-Agent", getUserAgent()) .build(); Response response = chain.proceed(compressedRequest); return response; } }; httpClient.interceptors().add(requestInterceptor); OkHttpClient okHttpClient = httpClient.build(); Retrofit client = new Retrofit.Builder() .baseUrl(AppConfig.Net.HOST) .addConverterFactory(new ScalarsConverterFactory()) .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient) .build(); //retrofit 没有日志打印功能 /*if (AppConfig.IS_DEVELOPING) { //日志可用 client.setLogLevel(RestAdapter.LogLevel.FULL); } else { client.setLogLevel(RestAdapter.LogLevel.NONE); }*/ api = client.create(RequestApi.class);而且retrofit2.0没有打印日志的功能,所以只能在okhttp的拦截器里打印日志
retrofit1.9的接口调用方式:
GET:
/** * 获得用户地址 */@GET("/user/Address?")void getAddress(Callback<AddressInfo>callback);
POST:
/** * 上传画作 * @param content 图集描述 * @param atk token值 * @param file 文件 */@Multipart@POST("/user/uploadPicture?")void upLoadPaint(@Part("content")String content, @Part("t")String atk, @Part("upfile") TypedFile file, Callback<BaseResponse> callback);而对应2.0之后的方式是这样的
GET:
/** * 获得用户地址 */@GET("/user/Address?")Calll<AddressInfo> getAddress();POST:
/** * 上传画作 * @param content 图集描述 * @param atk token值 * @param file 文件 */@Multipart@POST("/user/uploadPicture?")大致的升级过程算是完成了Call<BaseResponse> upLoadPaint(@Part("content")String content, @Part("t")String atk,@Part("upfile") RequestBody body);
接下来讲讲有什么坑:
1.纯粹使用retrofit接口返回不能是void ,而是相对应了Call<T>,接口的实现不在是直接使用Call回调,而是用getAddress().enqueue(new CallBack)
这里面的变动有点大,如果没做好回调函数的封装,那升级的工作量就有点大了,这样返回一个call可以用于取消请求。
2.在Retrofit 1.9中,如果获取的 response 不能背解析成定义好的对象,则会调用failure。但是在Retrofit 2.0中,不管 response 是否能被解析。onResponse总是会被调用。但是在结果不能被解析的情况下,response.body()会返回null。别忘了处理这种情况。
如果response存在什么问题,比如404什么的,onResponse也会被调用。你可以从response.errorBody().string()中获取错误信息的主体。
3.retrofit2.0去掉了一些类:RetrofitError,TypeFile MultipartTypedOutput,如果在1.9中用到,升级的时候需要注意
4.@Path注解在1.9的时候使用方式:
@Path(value="url", encode=false) String url
2.0时会导致encoded失效,此时推荐使用@Url动态替换url地址
@Multipart@POSTCall<String> upload(@Url String url, @PartMap Map<String, RequestBody> params);5.多图片上次的坑:MultipartTypedOutput,以及TypeFile在2.0之后被去除了
1.9的多图片上传方式:
/** * @param multipartTypedOutput 多个参数加载 */@POST("/user/applyPainting?")void request2BePaint(@Body MultipartTypedOutput multipartTypedOutput,Callback<BaseResponse> callback);multipartTypedOutput组装:
MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();multipartTypedOutput.addPart("t",new TypedString(painter.atk));multipartTypedOutput.addPart("content",new TypedString(painter.content));for (int i=0;i<painter.fileImages.length;i++){ multipartTypedOutput.addPart("upfile["+i+"]",new TypedFile("image/jpeg", new File(painter.fileImages[i])));}相对于简单些,而到2.0之后就感觉很繁琐
/** * 上传文件 */@Multipart@POSTCall<String> upload(@Url String url, @PartMap Map<String, RequestBody> params);组装map以及RequestBody
RequestBody requestFile = null;Map<String, RequestBody> photos = new HashMap<>();for (int i = 0; i < formFiles.size(); i++) { //生成临时压缩图片文件 final File tempFile = FileUtils.createFile(AppConfig.MAIN_DIR_IMG, "IMG_TEMP_" + System.currentTimeMillis() + ".jpg"); Bitmap bitmap = PhotoUtils.compressBitmap(formFiles.get(i).getFile().getPath()); FileOutputStream fos = new FileOutputStream(tempFile); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos); requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), tempFile); photos.put("AttachmentKey"+i+"\"; filename=\""+keys.get(i),requestFile);}以上就是我升级的过程,如有问题和错误请留言
1 0
- 升级okhttp3.0+和retrofit2.0+的过程以及遇到的坑
- 升级okhttp3.0+和retrofit2.0+的过程以及遇到的坑
- Retrofit2.0 + OkHttp3 遇到的坑
- Retrofit2.0+okhttp3缓存机制以及遇到的问题
- Retrofit2.0+okhttp3缓存机制以及遇到的问题
- rxjava2 + retrofit2 + okhttp3遇到的问题
- retrofit2.0+okhttp3+rxjava的封装(简单好用)
- retrofit2+okhttp3+ rxjava 遇到的问题及解决方案
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- Retrofit2.0+okhttp3设置缓存
- Retrofit2.0+okhttp3实现缓存
- Retrofit2.0使用过程中所踩到的坑
- 使用OkHttp3遇到的一些坑
- Android 给Retrofit2和OkHttp3添加统一的请求头Header
- Retrofit2.0的学习以及Rxjava与Retrofit2的结合使用
- Android中Scheme跳转协议
- 面向移动设备的HTML5开发框架
- 不同内网通信
- WebStorm 2016.2 版本发布
- C#: static关键字的作用
- 升级okhttp3.0+和retrofit2.0+的过程以及遇到的坑
- pdf在线转图片格式的方法
- ajax数据请求和多个音频的点击事件随笔
- Leetcode 237. Delete Node in a Linked List (Easy) (cpp)
- Android好书推荐:《Google Android SDK开发范例》
- Caffe学习系列:caffemodel可视化
- (JAVA)基于Socket的TCP和UDP编程
- 初学 python环境集成
- android中IBinder