1 想必各位android 開發(fā)人員 已經(jīng)對retrofit+rxjava +okhttp 都有所了解 就算是大家沒應(yīng)用 也有所耳聞了 因為本人現(xiàn)在正在寫一個新的工程 正在嘗試著使用 這個很高大上的框架 這這里給大家介紹一下 所遇到的問題 已經(jīng)新知識
基本創(chuàng)建 但是這對于我們一些應(yīng)用 需要參數(shù)加密 數(shù)據(jù)解密的開發(fā)者來講 這樣基本的創(chuàng)建是不足夠的 那么follow me
retrofit = new Retrofit.Builder()
.client(HttpConnection.getInstance())
.addConverterFactory(GsonConverterFactory.create())//轉(zhuǎn)換器
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //添加 RxJava 適配器
.baseUrl(Contacts.BASE_URL)
.build();
2 .自定義轉(zhuǎn)換器
- 繼承Converter.Factory
public final class DecodeConverterFactory extends Converter.Factory {
public static DecodeConverterFactory create() {
return create(new Gson());
}
public static DecodeConverterFactory create(Gson gson) {
return new DecodeConverterFactory(gson);
}
private final Gson gson;
private DecodeConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new DecodeRequestBodyConverter<>(gson, adapter);
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new DecodeResponseBodyConverter<>(adapter);
}
}
- 分別實現(xiàn)request,response轉(zhuǎn)換器
public class DecodeRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final Gson gson;
private final TypeAdapter<T> adapter;
DecodeRequestBodyConverter(Gson gson,TypeAdapter<T> adapter){
this.gson = gson;
this.adapter = adapter;
}
@Override
public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(),UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
adapter.write(jsonWriter,value);
jsonWriter.flush();
byte[] bytes=buffer.readByteArray();
try{
bytes=GzipUtil.compress(bytes);
}catch(Exception e){
e.printStackTrace();
}
bytes=new TripleDES (Contacts.PARAMETER_ENCRYPTION_KEY).encryptionByteData(bytes);
return RequestBody.create(MEDIA_TYPE,bytes);
}
}
public class DecodeResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final TypeAdapter<T> adapter;
DecodeResponseBodyConverter(TypeAdapter<T> adapter) {
this.adapter = adapter;
}
@Override
public T convert(ResponseBody value) throws IOException {
byte[] bytes = value.bytes();
//先解密 在解壓
try {
bytes= GzipUtil.unZip(new TripleDES(Contacts.BODY_ENCRYPTION_KEY).decryptionByteData(bytes));
} catch (Exception e) {
e.printStackTrace();
}
LogUtils.d(new String(bytes,"UTF-8"));
//解密字符串
return bytes==null?null:adapter.fromJson(new String(bytes,"UTF-8"));
}
}
-
retrofit的配置
對就是自定義的轉(zhuǎn)換器的名稱.png
- 接下來 說說我遇到的問題 我定義了自定義轉(zhuǎn)換器之后 發(fā)現(xiàn) 只是走解密 加密卻怎么都不走 自己一律了半天 才發(fā)現(xiàn) 自己的post 請求 標(biāo)簽 竟然寫成了@ Query 我忍不了自己了 這么二 竟然 這里要很注意的 @Body 這個標(biāo)簽
@POST("check")
Observable<DataVo<MessageVo,Object>> getTopMovie(@Body EquipmentParam equipment);