我们平时在开发迭代中,随着版本的更新以及需求的变化,可能这时候框架不能达到我们的要求,而这个时候另外有个框架可以达到,那我们如果要换的框架的话,这会涉及很多代码要改动,比如网络框架最早是用xutils,然后volley,现在大部分用的是okhttp,那么我们怎么用一行代码把app项目中的网络请求从volley切换到okhttp呢?而不是去全局改动代码,这样工作量就大了,今天就解决这个问题. 其实就是把请求抽象出来,然后利用java的多态根据设置的是用那个请求就行了.
public interface IHttpRequest { public void get(String url, Map,Object> params,ICallBack iCallBack); public void post(String url, Map ,Object> params,ICallBack iCallBack); }
这是抽取出来的,这里我们使用静态代理的方式:
public class HttpRequestProxy implements IHttpRequest { private static IHttpRequest iHttpRequest; private HttpRequestProxy() { } private static class Holder { private static final HttpRequestProxy INSTANCE = new HttpRequestProxy(); } public static HttpRequestProxy getInstance() { return Holder.INSTANCE; } public void init(NetRequestType netRequestType) { switch (netRequestType) { case OKHTTP: iHttpRequest = new OkHttpRequest(); break; case XUTIL: iHttpRequest = new XUtilsRequest(); break; case VOLLEY: iHttpRequest = new VolleyRequest(); break; } } @Override public void get(String url, Map, Object> params, ICallBack iCallBack) { if (null != iHttpRequest) iHttpRequest.get(url, params, iCallBack); } @Override public void post(String url, Map , Object> params, ICallBack iCallBack) { if (null != iHttpRequest) iHttpRequest.post(url, params, iCallBack); } }
然后其实我们只要针对不同的网络请求设置进来就ok了,比如使用Okhttp:
public class OkHttpRequest implements IHttpRequest { @Override public void get(String url, Map, Object> params, ICallBack iCallBack) { if(TextUtils.isEmpty(url)){ return; } StringBuffer stringBuffer = new StringBuffer(url); if(null!=params&&!params.isEmpty()){ stringBuffer.append("?"); for (Map.Entry , Object> entry : params.entrySet()) { stringBuffer.append(entry.getKey()).append("=").append(entry.getValue().toString()).append("&"); } stringBuffer.deleteCharAt(stringBuffer.length()-1); } OkHttpClient client = new OkHttpClient(); Log.e("OkHttpRequest","url="+stringBuffer.toString()); Request request = new Request.Builder().get().url(stringBuffer.toString()).build(); Call call = client.newCall(request); //异步调用并设置回调函数 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, final Response response) throws IOException { if(response.isSuccessful()){ String json = response.body().string(); Log.e("OkHttpRequest","json--->"+json); } } }); } @Override public void post(String url, Map , Object> params, ICallBack iCallBack) { } }
如果是volley的话,也是这么搞定的,其实就是里层封装好了,上层根据不同的需求,切换下就行了,在这还有请求回调以及json变成实体bean的
public interface ICallBack { void onSuccess(String json); void onFail(Exception e); }
在这把所有的请求结果全都是string,当然你也可以定义其他了,
public abstract class HttpCallBack<Result> implements ICallBack { @Override public void onSuccess(String json) { Gson gson = new Gson(); Class> clazz = getClassType(this); Result obj = (Result) gson.fromJson(json,clazz); onSuccess(obj); } public abstract void onSuccess(Result obj); private Class> getClassType(Object object) { if(null==object){ return null; } Type type = object.getClass().getGenericSuperclass(); Type[] params = ((ParameterizedType)type).getActualTypeArguments(); return (Class>) params[0]; } @Override public void onFail(Exception e) { } }
在这只是对返回结果的一种处理而已,当我们要进行切换的时候,只要在Application中调用一行代码:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); HttpRequestProxy.getInstance().init(NetRequestType.OKHTTP); } }
这就是网络框架的切换.这是架构师必懂的啊,可惜我是菜鸟!
上层调用:
Map,Object> params = new HashMap<>(); params.put("key","71e58b5b2f930eaf1f937407acde08fe"); params.put("num",20); HttpRequestProxy.getInstance().get("http://api.tianapi.com/social/", params, new HttpCallBack () { @Override public void onSuccess(Personal personal) { } @Override public void onFail(Exception e) { super.onFail(e); } });