使用场景:
甲方粑粑不积德,找外包公司给他们开发的时候,运气不好分配到实习生给你写SDK。然后可怕的就来了,这个SDK流落到你手上,让你来开发应用。
这就非常尴尬了。
该sdk让你这样用
BasicSdk sdk = new BasicSdk();
sdk.login(username,psw);
sdk.setOnlogin(new logincallback(){
onLogin(LoginResult result){
}
});
我当时就惊呆了,都0202年了,还有这种sdk吗。
但其实我们还是可以反抗一下的。用OkHttp的Interceptor。
比如说,参考他们sdk内部的实现,来实现我们的Interceptor逻辑
请求体的Interceptor
public class UserInterceptor implements Interceptor {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException{
Request request = chain.request();
Request.Builder builder = request.newBuilder();
//增加header,没问题
builder.addHeader("requestId", requestId)
.addHeader("token",token);
RequestBody body = request.body();
if (body instanceof FormBody) {
// 将参数组装成json格式
FormBody formBody = (FormBody) body;
JsonObject json = new JsonObject();
int size = formBody.size();
String name;
String value;
for (int i = 0; i < size; i++) {
// 有些服务器不识别转码之后的字符串
name = formBody.name(i);
value = formBody.value(i);
json.addProperty(name, value);
}
RequestBody newBody = RequestBody.create(JSON, json.toString());
builder.method("POST", newBody);
}
//把请求体,重新构建成自己想要的样子,并返回
return chain.proceed(builder.build());
}
}
返回体的Interceptor
public class ResponseInterceptor implements Interceptor {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
ResponseBody responseBody = response.body();
if (responseBody != null) {
String responseString = responseBody.string();
...实现自己的解析
ResponseBody newBody;
//也可以throw一个Exception让后面的处理
throw new CustomException()
....
//把解析后重新拼接的内容放newBody然后返回
return response.newBuilder()
.body(newBody)
.build();
}
结合retrofit使用
public class UserService extends BaseHttpsService{
// 保证只初始化一次,避免资源浪费
private static volatile UserService userService;
private UserApi api; //retrofit注解的接口
public UserApi getApi() {
return api;
}
private void initApi() {
Gson gson = new GsonBuilder()
.create();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//添加了请求体和返回体的拦截器
OkHttpClient httpClient =builder
.addInterceptor(AccountInterceptor.newInstance())
.addInterceptor(ResponseInterceptor.newInstance())
.addInterceptor(loggingInterceptor)
.build();
//构建retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(ServerConfig.URL) //也可以这样指定baseUrl
.client(httpClient)
.build();
api = retrofit.create(UserApi .class);
}
}
配置https的基类BaseHttpsService
public class BaseHttpsService {
protected OkHttpClient.Builder builder;
protected BaseHttpsService(){
X509TrustManager xtm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
};
SSLContext sslContext;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
SSLSocketFactory sslSocketFactory = new SSLSocketFactoryCompat(xtm);
HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true;
builder = new OkHttpClient.Builder()
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.sslSocketFactory(sslSocketFactory, xtm)
.hostnameVerifier(DO_NOT_VERIFY);
}
}
网友评论