分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

Okhttp

发布时间:2023-09-06 01:13责任编辑:苏小强关键词:http

https://github.com/hongyangAndroid/okhttputils

用法:

 compile ‘com.zhy:okhttputils:2.6.2‘

默认情况下,将直接使用okhttp默认的配置生成OkhttpClient,如果你有任何配置,记得在Application中调用initClient方法进行设置。

CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
???????OkHttpClient okHttpClient = new OkHttpClient.Builder()
// ???????????????.addInterceptor(new LoggerInterceptor("TAG"))
???????????????.connectTimeout(10000L, TimeUnit.MILLISECONDS)
???????????????.readTimeout(10000L, TimeUnit.MILLISECONDS)
???????????????.cookieJar(cookieJar)
???????????????.addInterceptor(new LoggerInterceptor("TAG"))
???????????????//其他配置
???????????????.build();
???????OkHttpUtils.initClient(okHttpClient);


OkHttpUtils
???????.get()
???????.url(UrlBuilder.URL + url)
???????.params(hashMap)
???????.build()
???????.execute(new StringCallback() {
???????????@Override
???????????public void onError(Call call, Exception e, int id) {
???????????????ToastUtil.makeToast("请求异常" + e.toString());
???????????????LogUtil.e(requstTitle + "请求异常" + e.toString());
???????????????requstResult.failure();
???????????}

???????????@Override
???????????public void onResponse(String response, int id) {
???????????????resultResponse(response);
???????????}

???????});
OkHttpUtils
???????.post()
???????.url(UrlBuilder.URL + url)
???????.params(hashMap)
???????.build()
???????.execute(new StringCallback() {
???????????@Override
???????????public void onError(Call call, Exception e, int id) {
???????????????ToastUtil.makeToast("请求异常" + e.toString());
???????????????LogUtil.e(requstTitle + "请求异常" + e.toString());
???????????????requstResult.failure();
???????????}

???????????@Override
???????????public void onResponse(String response, int id) {
???????????????resultResponse(response);
???????????}

???????});
JSONObject jsonObject = new JSONObject(hashMap);
String json = jsonObject.toString();
OkHttpUtils
???????.postString()
???????.url(UrlBuilder.URL + url)
???????.content(json)
???????.build()
???????.execute(new StringCallback() {
???????????@Override
???????????public void onError(Call call, Exception e, int id) {
???????????????ToastUtil.makeToast("请求异常" + e.toString());
???????????????LogUtil.e(requstTitle + "请求异常" + e.toString());
???????????????requstResult.failure();
???????????}

???????????@Override
???????????public void onResponse(String response, int id) {
???????????????resultResponse(response);
???????????}

???????});
 

Okhttp

原文地址:http://www.cnblogs.com/huihuizhang/p/7593031.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved