博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
okhttp3 get post 简单封装
阅读量:5057 次
发布时间:2019-06-12

本文共 2929 字,大约阅读时间需要 9 分钟。

最近打算在新项目中使用 okhttp3, 简单封装了一下异步 get post 

因为 CallBack 也是在子线程中执行,所以用到了 Handler

public class MyOkHttpClient {    private static MyOkHttpClient myOkHttpClient;    private OkHttpClient okHttpClient;    private Handler handler;    private MyOkHttpClient() {        okHttpClient = new OkHttpClient();        handler = new Handler(Looper.getMainLooper());    }    public static MyOkHttpClient getInstance() {        if (myOkHttpClient == null) {            synchronized (MyOkHttpClient.class) {                if (myOkHttpClient == null) {                    myOkHttpClient = new MyOkHttpClient();                }            }        }        return myOkHttpClient;    }    class StringCallBack implements Callback {        private HttpCallBack httpCallBack;        private Request request;        public StringCallBack(Request request, HttpCallBack httpCallBack) {            this.request = request;            this.httpCallBack = httpCallBack;        }        @Override        public void onFailure(Call call, IOException e) {            final IOException fe = e;            if (httpCallBack != null) {                handler.post(new Runnable() {                    @Override                    public void run() {                        httpCallBack.onError(request, fe);                    }                });            }        }        @Override        public void onResponse(Call call, Response response) throws IOException {            final String result = response.body().string();            if (httpCallBack != null) {                handler.post(new Runnable() {                    @Override                    public void run() {                        httpCallBack.onSuccess(request, result);                    }                });            }        }    }    public void asyncGet(String url, HttpCallBack httpCallBack) {        Request request = new Request.Builder().url(url).build();        okHttpClient.newCall(request).enqueue(new StringCallBack(request, httpCallBack));    }    public void asyncPost(String url, FormBody formBody, HttpCallBack httpCallBack) {        Request request = new Request.Builder().url(url).post(formBody).build();        okHttpClient.newCall(request).enqueue(new StringCallBack(request, httpCallBack));    }    public interface HttpCallBack {        void onError(Request request, IOException e);        void onSuccess(Request request, String result);    }}

使用

FormBody formBody = new FormBody.Builder().add("username", "lesiefang").add("age", "28").build();MyOkHttpClient.getInstance().asyncPost("http://192.168.94.131:5000/aaa/", formBody, new MyOkHttpClient.HttpCallBack() {    @Override    public void onError(Request request, IOException e) {        Log.d("aaa", e.toString());    }    @Override    public void onSuccess(Request request, String result) {        textView.setText(result);    }});

至于上传文件、下载文件什么的用到时再封装一个类似的方法就行了

 

转载于:https://www.cnblogs.com/lesliefang/p/5433596.html

你可能感兴趣的文章
Can't create handler inside thread that has not called Looper.prepare()
查看>>
图像的双缓存技术
查看>>
微信小程序template模板与component组件的区别及使用方法
查看>>
通过机构查询该机构下,以及下级机构的人员 id
查看>>
好程序员Python自动化运维开发实战 六、流程控制
查看>>
密码生成器
查看>>
制作TortoiseSVN最新版本的中文DLL(转)
查看>>
最小生成树 Prim算法 Kruskal算法实现
查看>>
javaee字符文件的复制
查看>>
选项框
查看>>
android 开发之 - 调用系统闪光灯
查看>>
HTML标签
查看>>
类型转换,随机数
查看>>
clang complete
查看>>
在Ajax1.0中使用Response.Redirect()
查看>>
lambdas了解
查看>>
程序员从初级到中级10个秘诀
查看>>
最新 Java 漏洞攻击以微软通知做诱饵
查看>>
对理想团队模式构建的设想以及对软件流程的理解
查看>>
Android反编译后代码阅读
查看>>