IT源码网

封装安卓的okhttp

xmjava 2021年02月16日 手机开发 416 0

1.封装了get方法,handler更新主线程,回调的onsuccess,onfailure,onerror等方法

2.配置文件

api 'com.android.support:recyclerview-v7:26.1.0' 
api 'com.squareup.okhttp3:okhttp:3.9.1' 
//fastjson 
api 'com.alibaba:fastjson:1.2.44'

 

3.先建立一个http包,建BaseHttpCallback和OkHttpHelper这2个类,以后会加loading...这个东西

 

 

public class OkHttpHelper { 
 
    private static OkHttpClient okHttpClient; 
    private android.os.Handler handler; 
 
    private OkHttpHelper(){ 
        okHttpClient =new OkHttpClient(); 
        handler =new android.os.Handler(Looper.getMainLooper()); 
 
    }; 
 
    public static OkHttpHelper getInstance(){ 
        return new OkHttpHelper(); 
    } 
    public void get(String url,BaseHttpCallback baseHttpCallback){ 
        Request request = buildRequest(url,null,HttpMethodType.GET); 
        doRequest(request, baseHttpCallback); 
    } 
 
    public void post(String url, Map<String,String>params,BaseHttpCallback baseHttpCallback){ 
        Request request = buildRequest(url,null,HttpMethodType.POST); 
        doRequest(request, baseHttpCallback); 
    } 
 
    public void doRequest(final Request request, final BaseHttpCallback baseHttpCallback){ 
 
        baseHttpCallback.onRequestBefore(request); 
        okHttpClient.newCall(request).enqueue(new Callback() { 
            @Override 
            public void onFailure(Call call, IOException e) { 
                callbackFailure(baseHttpCallback,e); 
            } 
            @Override 
            public void onResponse(Call call, Response response) throws IOException { 
                if(response.isSuccessful()){ 
                    String s= response.body().string(); 
                    callbackSuccess(baseHttpCallback,response,s); 
                }else{ 
                    callbackError(baseHttpCallback,response,response.code()); 
                } 
            } 
        }); 
    } 
 
    private Request buildRequest(String url,Map<String,String> params,HttpMethodType methodType){ 
        Request.Builder builder = new Request.Builder(); 
        builder.url(url); 
        if(methodType==HttpMethodType.GET){ 
            builder.get(); 
        }else if(methodType==HttpMethodType.POST){ 
 
        } 
        return builder.build(); 
    } 
 
    enum HttpMethodType{ 
        GET, 
        POST 
    } 
 
 
    private void callbackSuccess(final BaseHttpCallback callback, final Response response, final String s){ 
        handler.post(new Runnable() { 
            @Override 
            public void run() { 
                callback.onSuccess(response,s); 
            } 
        }); 
    } 
 
 
    private void callbackError(final BaseHttpCallback callback, final Response response, final int code){ 
        handler.post(new Runnable() { 
            @Override 
            public void run() { 
                callback.onError(response,code,null); 
            } 
        }); 
    } 
 
    private void callbackFailure(final BaseHttpCallback callback, final Exception e){ 
        handler.post(new Runnable() { 
            @Override 
            public void run() { 
                callback.onFailure(e); 
            } 
        }); 
    } 
 
}
public abstract class BaseHttpCallback { 
 
    public abstract void onRequestBefore(Request request); 
 
    public abstract void onSuccess(Response response,String string); 
    public abstract void onError(Response response,int code,Exception e); 
 
    public abstract void onFailure(Exception e); 
}

4. 还另外封装了个User类

public class User { 
    public String result; 
    public List<Data> data; 
    public class Data{ 
        public String name; 
        public String age; 
 
        public String getName() { 
            return name; 
        } 
 
        public String  getAge() { 
            return age; 
        } 
    } 
    public String getResult() { 
        return result; 
    } 
    public List<Data> getDatas() { 
        return data; 
    } 
}

 

5.activity中调用例子

OkHttpHelper httpHelper  =OkHttpHelper.getInstance(); 
 String url = "http://www.baidu.com"; 
        httpHelper.get(url,new BaseHttpCallback(){ 
 
            @Override 
            public void onRequestBefore(Request request) { 
 
            } 
 
            @Override 
            public void onSuccess(Response response, String string) { 
 
            } 
 
            @Override 
            public void onError(Response response, int code, Exception e) { 
 
            } 
 
            @Override 
            public void onFailure(Exception e) { 
                Toast.makeText(MainActivity.this,"当前网络不可用,请检查网络",Toast.LENGTH_LONG).show(); 
            } 
        });

 

6.未完善的,没有post,没有loading效果,

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!