前面已经讲了
Retrofit 2.0 详解(一)基本用法
Retrofit 2.0 详解(二)加载https请求
接下来我们讲解一下怎么通过Retrofit 来个传输中的报文加密,老规矩,还是先写Service
接口
@POST("NewsServlet")
Observable<String> testInterceptor(@Body News news);
关键点就是怎么把news
这个对象的json
字符串加密,那就需要用到拦截器了Interceptor
,不知道的老铁还是自行百度吧,下面咱们看下怎么通过Interceptor
来给报文加解密的。
第一步,就要实现Interceptor
接口,重写Response intercept(Chain chain)
方法
public class EncryptInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//这个是请求的url,也就是咱们前面配置的baseUrl
String url = request.url().toString();
//这个是请求方法
String method = request.method();
long t1 = System.nanoTime();
request = encrypt(request);//模拟的加密方法
Response response = chain.proceed(request);
long t2 = System.nanoTime();
response = decrypt(response);
return response;
}
}
下面咱们看下加密方法是怎么写的
//加密
private Request encrypt(Request request) throws IOException {
//获取请求body,只有@Body 参数的requestBody 才不会为 null
RequestBody requestBody = request.body();
if (requestBody != null) {
okio.Buffer buffer = new okio.Buffer();
requestBody.writeTo(buffer);
Charset charset = Charset.forName("UTF-8");
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
String string = buffer.readString(charset);
//模拟加密的方法,这里调用大家自己的加密方法就可以了
String encryptStr = encrypt(string);
RequestBody body = MultipartBody.create(contentType, encryptStr);
request = request.newBuilder()
.post(body)
.build();
}
return request;
}
咱们看下String string = buffer.readString(charset)
;这个string
读出来的是什么
{"comments":[],"date":"20170619","id":1,"likes":"likes","title":"Converter用法3","views":"views"}
其实就是把news转换成了json字符串
下面看下我随便写的加密方法,前面随便加个"我是密文:"
//模拟加密的方法
private String encrypt(String string) {
return "我是密文:" + string;
}
在看下服务器收到的报文
我是密文:{"comments":[],"date":"20170619","id":1,"likes":"likes","title":"Converter用法3","views":"views"}
下面就是服务解密做逻辑处理了,做完处理服务器在给客户端响应的时候也续要加密返回和客户端,下面来看下客户端是怎么解密的
private Response decrypt(Response response) throws IOException {
if (response.isSuccessful()) {
//the response data
ResponseBody body = response.body();
BufferedSource source = body.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
Charset charset = Charset.defaultCharset();
MediaType contentType = body.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
String string = buffer.clone().readString(charset);
//解密方法,需要自己去实现
String bodyString = decrypt(string);
ResponseBody responseBody = ResponseBody.create(contentType, bodyString);
response = response.newBuilder().body(responseBody).build();
}
return response;
}
后来响应的报文
我是密文:{"comments":[],"date":"20170619","id":1,"likes":"likes","title":"Converter用法3","views":"views"}
我这边的解密方法和也很简单
//模拟解密的方法
private String decrypt(String string) {
if (string != null && string.length() != 0) {
string.replace("我是密文:", "");
}
return string;
}
第二步添加Interceptor
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.addInterceptor(new EncryptInterceptor());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
//可以接收自定义的Gson,当然也可以不传
.addConverterFactory(GsonConverterFactory.create(gson))
// 针对rxjava2.x
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(builder.build())
.build();
第三步 调用
NewsService newsService = createRetrofit().create(NewsService.class);
News news = new News();
news.likes = "likes";
news.date = "20170619";
news.title = "Converter用法3";
news.id = 1;
news.views = "views";
Observable<String> observable = newsService.testInterceptor(news);
observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String value) {
Log("Scalar用法 onNext value:" + value);
}
@Override
public void onError(Throwable e) {
Log("Scalar用法 onError " + e.getMessage());
e.printStackTrace();
}
@Override
public void onComplete() {
}
});
这样就完成了加密解密了,外面调用不用关心里面的加密解密逻辑,只需要按照以前的逻辑开发就可以了。