为什么要隔离第三方框架
其实就是为了代码维护起来比较方便,如果不进行第三方框架的隔离,我们的应用架构应该是这样的。
假如说这时候,框架版本迭代,或者后台接口改变,或者说需要更换第三方框架,那么就要大动干戈。
那么我们应该如何设计才能让上述操作不要大动干戈呢?
我们添加一个中间层,通过这个中间层将调用方和执行方(第三方框架)隔离,使用者不需要关心谁来完成工作,执行者只需要专注自己的任务
这里我们将 AsyncHttp 框架进行隔离。
/**
* 接口,定义了 get 和 post 方法
*/
public interface HttpRequest {
void get(String url,Map<String,String> params,ICallback iCallback);
void post(String url,Map<String,String> params,ICallback iCallback);
}
/**
* 回调接口,定义了成功的回调和失败的回调
*/
public interface ICallback {
void onSuccess(String result);
void onFailure(int code,Throwable t);
}
/**
* 委托类,真正实现访问网络的类
*/
public class AsynHttpRequest implements HttpRequest {
public AsyncHttpClient asyncHttpClient;
public AsynHttpRequest(){
this.asyncHttpClient = new AsyncHttpClient();
}
@Override
public void get(String url, Map<String, String> params, final ICallback iCallback) {
StringBuffer sb = new StringBuffer("?");
//拼接get 请求
if(null != params){
Set<Map.Entry<String, String>> entries = params.entrySet();
for(Map.Entry<String,String> entry : entries){
sb.append(entry.getKey());
sb.append("=");
sb.append(entry.getValue());
sb.append("&");
}
}
sb.deleteCharAt(sb.length() - 1);
url += sb.toString();
asyncHttpClient.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
iCallback.onSuccess(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
iCallback.onFailure(statusCode,error);
}
});
}
@Override
public void post(String url, Map<String, String> params, final ICallback iCallback) {
}
}
/**
* 这个就是中间层,代理类
*/
public class HttpRequestPresenter implements HttpRequest{
//代理类持有委托类的实例
private HttpRequest httpRequest;
private static volatile HttpRequestPresenter instance;
private HttpRequestPresenter(HttpRequest httpRequest){
this.httpRequest = httpRequest;
}
public static void init(HttpRequest httpRequet){
if(instance == null){
synchronized (HttpRequestPresenter.class){
if(instance == null){
instance = new HttpRequestPresenter(httpRequet);
}
}
}
}
public static HttpRequestPresenter getInstance(){
return instance;
}
@Override
public void get(String url, Map<String, String> params, ICallback iCallback) {
//具体的执行还是要为拖类自己来完成
httpRequest.get(url,params,iCallback);
}
@Override
public void post(String url, Map<String, String> params, ICallback iCallback) {
httpRequest.post(url,params,iCallback);
}
}
//具体的使用
HttpRequestPresenter.init(new AsynHttpRequest());
Map<String,String> params = new HashMap<>();
params.put("city","杭州");
params.put("key","13cb58f5884f9749287abbead9c658f2");
HttpRequestPresenter.getInstance().get("http://restapi.amap.com/v3/weather/weatherInfo"
, params, new ICallback() {
@Override
public void onSuccess(String result) {
Log.i(TAG, "onSuccess: "+result);
}
@Override
public void onFailure(int code, Throwable t) {
Log.e(TAG, "onFailure: "+t.toString());
}
});
如果我们不想返回json,而是直接返回实体类,继续写下面的代码
public abstract class ModelCallback<T> implements ICallback {
@Override
public void onSuccess(String result) {
Class<? extends T> geneticClass = getGeneticClass(this);
//通过 gson 转换成对象
T t = new Gson().fromJson(result,geneticClass);
//重定向到另一个 onSuccess()方法中
onSuccess(t);
}
private Class<? extends T> getGeneticClass(Object object) {
//获得带有泛型的直接父类type
Type genericSuperclass = object.getClass().getGenericSuperclass();
//ParameterizedType 带参数的类型 泛型
//getActualTypeArguments 参数的类型,泛型类型
return (Class<? extends T>) ((ParameterizedType)genericSuperclass).getActualTypeArguments()[0];
}
public abstract void onSuccess(T t);
}
在使用的时候改成如下代码即可
HttpRequestPresenter.getInstance().get("http://restapi.amap.com/v3/weather/weatherInfo"
, params, new ModelCallback<WeatherInfo>() {
@Override
public void onSuccess(WeatherInfo weatherInfo) {
}
@Override
public void onFailure(int code, Throwable t) {
}
});