Android-接口编程--接口的妙用

比如一个网络加载框架,可以使用Okhttp Retrofit Volly 怎么才能实现一分钟实现网络框架的切换

首先一个网络请求需要:1 url,params,callback先定义一个万能接口和一个回调

public interface IomnipotentHttpIml {
    void post(String url, Map params,ICallBack callBack);
}
public interface ICallBack {
    void onSuccess(String result);
    void onFailed();
}

然后创建一个万能网络管理类 IomnipotentHttpManger

 

public class IomnipotentHttpManger implements IomnipotentHttpIml {
    private static IomnipotentHttpManger sInstance;
    private static IomnipotentHttpIml mIOmnipotentIml;

    public static IomnipotentHttpManger getInstance() {
        if (sInstance == null) {
            synchronized (IomnipotentHttpManger.class) {
                if (sInstance == null) {
                    sInstance = new IomnipotentHttpManger();
                }
            }
        }
        return sInstance;
    }

    public static void init(IomnipotentHttpIml net) {
        mIOmnipotentNet = net;
    }

    @Override
    public void post(String url, Map params, ICallBack callBack) {
        mIOmnipotentIml.post(url, params, callBack);
    }
}

比如我们用Okhttp来请求一个网络请求

先创建一个OkhttpRequest 实现我们的成能接口,并实现其post方法,一个最基本的请求方法

public class OkhttpRequest implements IomnipotentHttpIml {
     
    private OkHttpClient mOkHttpClient;

    public OkhttpRequest() {
        mOkHttpClient = new OkHttpClient.Builder().build();

    }


    @Override
    public void post(String url, Map params, ICallBack callBack) {
        RequestBody requestBody = appentBody(params);
        Request request = new Request.Builder().url(url).post(requestBody).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful()) {
                    assert response.body() != null;
                    String string = response.body().string();
                    callBack.onSuccess(string);
                }
            }
        });
    }

    private RequestBody appentBody(Map params) {
        FormBody.Builder builder = new FormBody.Builder();
        if (params == null || params.isEmpty()) {
            return builder.build();
        }

        for (Map.Entry entry : params.entrySet()) {
            builder.add(entry.getKey(), entry.getValue().toString());
        }
        return builder.build();
    }
}

 

public abstract class HttpCallBack implements ICallBack {
    @Override
    public void onSuccess(String result) {
        Gson gson = new Gson();
        Class clazz = analysClassInfo(this);

        Result response = (Result) gson.fromJson(result, clazz);
        onSuccess(response);
    }

    public abstract void onSuccess(Result result);

    private Class analysClassInfo(Object object) {
        Type type = object.getClass().getGenericSuperclass();
        assert type != null;
        return (Class) ((ParameterizedType)type).getActualTypeArguments()[0];
    }

    @Override
    public void onFailed() {

    }
}

 

怎么使用呢,我们在Application类里面

IomnipotentHttpManger.init(new OkhttpRequest());//这样子当我们调用IomnipotentHttpManger.post()请求数据的时候用的就是我们OkhttpRequest的post方法

改起来就相当的方便

你可能感兴趣的:(Android,okhttp3)