org.xutils.http.app.ResponseParser&DefaultParamsBuilder
package org.xutils.http.app;
import org.xutils.http.request.UriRequest;
import java.lang.reflect.Type;
/**
* @author 注释者:王教成
* @version 注释版:1.0.0
* 响应分析器接口,{@link org.xutils.http.annotation.HttpResponse}注解的返回值转换模板
*/
public interface ResponseParser {
/**
* 检查请求相应头等处理
* @param request Uri请求
* @throws Throwable 抛出异常
*/
void checkResponse(UriRequest request) throws Throwable;
/**
* result字符串转换为resultType类型对象
* @param resultType 返回值类型(可能带有泛型信息)
* @param resultClass 返回值类
* @param result 字符串
* @return 返回Type类型对象
* @throws Throwable 抛出异常
*/
Object parse(Type resultType, Class<?> resultClass, String result) throws Throwable;
}package org.xutils.http.app;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.http.annotation.HttpRequest;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* @author 注释者:王教成
* @version 注释版:1.0.0
* 默认参数构造器
*/
public class DefaultParamsBuilder implements ParamsBuilder {
public DefaultParamsBuilder() {
}//构造器
/**
* 根据网络请求注解,构建请求的url地址字符串
* @param params 请求参数
* @param httpRequest 网络请求注解
* @return 返回url地址字符串
* @throws Throwable 抛出异常
*/
@Override
public String buildUri(RequestParams params, HttpRequest httpRequest) throws Throwable {
return httpRequest.host() + "/" + httpRequest.path();//返回主机+路径
}
/**
* 构建缓存键,根据注解的cacheKeys构建缓存的自定义key,如果返回null,默认使用url和整个query string组成
* @param params 请求参数
* @param cacheKeys 缓存键字符串数组
* @return 返回缓存键字符串
*/
@Override
public String buildCacheKey(RequestParams params, String[] cacheKeys) {
String cacheKey = null;//声明缓存键
if (cacheKeys != null && cacheKeys.length > 0) {
cacheKey = params.getUri() + "?";//如果缓存键非空且长度大于0,获取Uri+?缓存键
for (String key : cacheKeys) {
String value = http://www.gunmi.cn/v/params.getStringParameter(key);//迭代缓存键获取字符串参数值
if (value != null) {
cacheKey += key + "=" + value + "&";//如果字符串参数值非空,缓存键叠加键值
}
}// 添加cacheKeys对应的参数代码块
}
return cacheKey;
}
/**
* 构建请求参数,为请求添加通用参数等操作(实现空方法)
* @param params 请求参数
* @throws Throwable 抛出异常
*/
@Override
public void buildParams(RequestParams params) throws Throwable {
}
/**
* 构建标记,自定义参数签名(实现空方法)
* @param params 请求参数
* @param signs 标记字符串数组
* @throws Throwable 抛出异常
*/
@Override
public void buildSign(RequestParams params, String[] signs) throws Throwable {
}
/**
* 获取自定义SSLSocketFactory
* @return 返回自定义SSLSocketFactory
* @throws Throwable 抛出异常
*/
@Override
public SSLSocketFactory getSSLSocketFactory() throws Throwable {
return getTrustAllSSLSocketFactory();//调用本类获取信任所有SSLSocket工厂方法
}
private static SSLSocketFactory trustAllSSlSocketFactory;//声明信任所有SSLSocket工厂
/**
* 获取信任所有SSLSocket工厂
* @return 返回信任所有SSLSocket工厂
*/
public static SSLSocketFactory getTrustAllSSLSocketFactory() {
if (trustAllSSlSocketFactory == null) {
synchronized (DefaultParamsBuilder.class) {
if (trustAllSSlSocketFactory == null) {
//信任所有证书代码块
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;//返回null
}//获取接受发行人,返回X509证书数组
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}//检查客户端已信任,以X509整数数组和授权类型字符串为参数
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}//检查服务器已信任,以X509整数数组和授权类型字符串为参数
}};//以新建X509信任管理器为参数,创建信任所有证书的信任管理器,重写X509信任管理器接口三个方法
try {
SSLContext sslContext = SSLContext.getInstance("TLS");//创建SSLContext实例
sslContext.init(null, trustAllCerts, null);//以X509证书数组为参数初始化SSLContext
trustAllSSlSocketFactory = sslContext.getSocketFactory();//用SSLContext获取Socket工厂
} catch (Throwable ex) {
LogUtil.e(ex.getMessage(), ex);//捕获异常记录日志
}
}//如果信任所有SSLSocket工厂非空,同步锁默认参数生成器类
}
}
return trustAllSSlSocketFactory;
}
}