包名变化
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(Callbackcallback);
/** * 上传画作 * @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而对应2.0之后的方式是这样的callback);
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") R equestBody body) ; 大致的升级过程算是完成了
接下来讲讲有什么坑:
1.纯粹使用retrofit接口返回不能是void ,而是相对应了Call
这里面的变动有点大,如果没做好回调函数的封装,那升级的工作量就有点大了,这样返回一个call可以用于取消请求。
2.在Retrofit 1.9中,如果获取的 response 不能背解析成定义好的对象,则会调用failure。但是在Retrofit 2.0中,不管 response 是否能被解析。onResponse总是会被调用。但是在结果不能被解析的情况下,response.body()会返回null。别忘了处理这种情况。
@Path(value="url", encode=false) String url
2.0时会导致encoded失效,此时推荐使用@Url动态替换url地址
@Multipart @POST Call5.多图片上次的坑: MultipartTypedOutput,以及TypeFile在2.0之后被去除了upload(@Url String url, @PartMap Map , RequestBody> params);
1.9的多图片上传方式:
/** * @param multipartTypedOutput 多个参数加载 */ @POST("/user/applyPainting?") void request2BePaint(@Body MultipartTypedOutput multipartTypedOutput,CallbackmultipartTypedOutput组装:callback);
MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput(); multipartTypedOutput.addPart("t",new TypedString(painter.atk)); multipartTypedOutput.addPart("content",new TypedString(painter.content)); for (int i=0;i相对于简单些,而到2.0之后就感觉很繁琐fileImages.length;i++){ multipartTypedOutput.addPart("upfile["+i+"]",new TypedFile("image/jpeg", new File(painter.fileImages[i]))); }
/** * 上传文件 */ @Multipart @POST Call组装map以及RequestBodyupload(@Url String url, @PartMap Map , RequestBody> params);
RequestBody requestFile = null; Map以上就是我升级的过程,如有问题和错误请留言, 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);}