一行代码实现网络框架的切换 (网络)

使用代理模式

数据回调接口

public interface ICallback {
//为了适应各种网络框架的不同的返回类型使用String来接收
    void onSuccess(String result);
    void onFailure(String e);
}

功能接口

//使用一种举例
public interface IHttpProcessor {
    void post(String url, Map params,ICallback callback);
}

代理类

public class HttpHelper implements IHttpProcessor {

    //单例
    private static HttpHelper httpHelper;
    public static HttpHelper getInstance(){
        synchronized (HttpHelper.class){
            if(httpHelper==null){
                httpHelper = new HttpHelper();
            }
        }
        return httpHelper;
    }
    private HttpHelper(){}
    private static IHttpProcessor mIHttpProcessor = null;
    //定义一个用于设置代理的接口
    public static void init(IHttpProcessor httpProcessor){
        mIHttpProcessor = httpProcessor;
    }

    @Override
    public void post(String url, Map params, ICallback callback) {
        mIHttpProcessor.post(url,params,callback);
    }
}

使用两种框架来举例说明(Volley xUtils)

//volley
public class VolleyProcessor implements IHttpProcessor{
    private static RequestQueue mQueue = null;
    public VolleyProcessor(Context context){
        mQueue = Volley.newRequestQueue(context);
    }

    /**
     * 真实的网络操作
     * @param url
     * @param params
     * @param callback
     */
    @Override
    public void post(String url, Map params, final ICallback callback) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener() {
            @Override
            public void onResponse(String response) {
                callback.onSuccess(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callback.onFailur(error.getMessage());
            }
        });
        mQueue.add(stringRequest);
    }
}

//xUtils
public class XUtilsProcessor implements IHttpProcessor{

    public XUtilsProcessor(MyApplication application){
        x.Ext.init(application);
    }

    /**
     * 真实的网络操作
     * @param url
     * @param params
     * @param callback
     */
    @Override
    public void post(String url, Map params, final ICallback callback) {
        RequestParams requestParams = new RequestParams(url);
        x.http().post(requestParams, new Callback.CommonCallback() {
            @Override
            public void onSuccess(String result) {
                callback.onSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}

回调接口实现 json转换bean

public abstract class HttpCallback implements ICallback{

    @Override
    public void onSuccess(String result) {
        //网络响应的数据
        Gson gson = new Gson();
        //得到使用者用?javaBean来接收数据
        Class clazz = getClassInfo(this);
       Result objResult = (Result)gson.fromJson(result,clazz);
       //将数据返回
        onSuccess(objResult);
    }

    public abstract void onSuccess(Result objresult);

    private Class getClassInfo(Object object) {
        //通过getGenericSuperclass返回一个类型对象
        Type getType = object.getClass().getGenericSuperclass();
        Type[] typeArguments = ((ParameterizedType) getType).getActualTypeArguments();
        return (Class) typeArguments[0];
    }
}

在application中就可以完成框架切换了

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //使用一行代码进行网络框架切换
        // HttpHelper.init(new VolleyProcessor(this));
        HttpHelper.init(new XUtilsProcessor(this));
    }
}

这里只使用了两种网络框架来举例说明,要使用另外的框架只需添加实现类即可,这种方法可以大大减少项目中因框架的更换来带来的大量代码修改。

你可能感兴趣的:(网络)