httpRequest网络请求
package com.zhy.http.okhttp; import android.os.Handler; import android.os.Looper; import android.text.TextUtils; import android.util.Log; import com.zhy.http.okhttp.cookie.SimpleCookieJar; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Response; import com.zhy.http.okhttp.builder.GetBuilder; import com.zhy.http.okhttp.builder.PostFileBuilder; import com.zhy.http.okhttp.builder.PostFormBuilder; import com.zhy.http.okhttp.builder.PostStringBuilder; import com.zhy.http.okhttp.callback.Callback; import com.zhy.http.okhttp.https.HttpsUtils; import com.zhy.http.okhttp.request.RequestCall; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.TimeUnit; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; /** * Created by zhy on 15/8/17. */ public class OkHttpUtils { public static final String TAG = "OkHttpUtils"; public static final long DEFAULT_MILLISECONDS = 10000; private static OkHttpUtils mInstance; private OkHttpClient mOkHttpClient; private Handler mDelivery; private OkHttpUtils() { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); //cookie enabled okHttpClientBuilder.cookieJar(new SimpleCookieJar()); mDelivery = new Handler(Looper.getMainLooper()); if (true) { okHttpClientBuilder.hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); } mOkHttpClient = okHttpClientBuilder.build(); } private boolean debug; private String tag; public OkHttpUtils debug(String tag) { debug = true; this.tag = tag; return this; } public static OkHttpUtils getInstance() { if (mInstance == null) { synchronized (OkHttpUtils.class) { if (mInstance == null) { mInstance = new OkHttpUtils(); } } } return mInstance; } public Handler getDelivery() { return mDelivery; } public OkHttpClient getOkHttpClient() { return mOkHttpClient; } public static GetBuilder get() { return new GetBuilder(); } public static PostStringBuilder postString() { return new PostStringBuilder(); } public static PostFileBuilder postFile() { return new PostFileBuilder(); } public static PostFormBuilder post() { return new PostFormBuilder(); } public void execute(final RequestCall requestCall, Callback callback) { if (debug) { if(TextUtils.isEmpty(tag)) { tag = TAG; } Log.d(tag, "{method:" + requestCall.getRequest().method() + ", detail:" + requestCall.getOkHttpRequest().toString() + "}"); } if (callback == null) callback = Callback.CALLBACK_DEFAULT; final Callback finalCallback = callback; requestCall.getCall().enqueue(new okhttp3.Callback() { @Override public void onFailure(Call call, final IOException e) { sendFailResultCallback(call, e, finalCallback); } @Override public void onResponse(final Call call, final Response response) { if (response.code() >= 400 && response.code() <= 599) { try { sendFailResultCallback(call, new RuntimeException(response.body().string()), finalCallback); } catch (IOException e) { e.printStackTrace(); } return; } try { Object o = finalCallback.parseNetworkResponse(response); sendSuccessResultCallback(o, finalCallback); } catch (Exception e) { sendFailResultCallback(call, e, finalCallback); } } }); } public void sendFailResultCallback(final Call call, final Exception e, final Callback callback) { if (callback == null) return; mDelivery.post(new Runnable() { @Override public void run() { callback.onError(call, e); callback.onAfter(); } }); } public void sendSuccessResultCallback(final Object object, final Callback callback) { if (callback == null) return; mDelivery.post(new Runnable() { @Override public void run() { callback.onResponse(object); callback.onAfter(); } }); } public void cancelTag(Object tag) { for (Call call : mOkHttpClient.dispatcher().queuedCalls()) { if (tag.equals(call.request().tag())) { call.cancel(); } } for (Call call : mOkHttpClient.dispatcher().runningCalls()) { if (tag.equals(call.request().tag())) { call.cancel(); } } } public void setCertificates(InputStream... certificates) { mOkHttpClient = getOkHttpClient().newBuilder() .sslSocketFactory(HttpsUtils.getSslSocketFactory(certificates, null, null)) .build(); } public void setConnectTimeout(int timeout, TimeUnit units) { mOkHttpClient = getOkHttpClient().newBuilder() .connectTimeout(timeout, units) .build(); } }
- 1
- 粉丝: 2
- 资源: 3
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于MATLAB的数字滤波器设计及其语音信号去噪应用 (供学习交流) 其中数字滤波器包括IIR和FIR的低通、高通、带通、带阻四大类型及其多种设计方法 GUI界面中有语音信号输入模块,滤波
- 基于SpringBoot框架开发的博客系统,采用了springBoot、springSecurity、Redis、Mybatis、Nginx等技术,主要实现了博
- 基于VLC的第三方播放器,基于各种格式的视频解码,资料齐全+文档+源码.zip
- 基于WebClient实现的爬虫,可以抓取JS动态生成的内容资料齐全+文档+源码.zip
- 基于UICrawler开源工程,开发的针对android APP 自动化遍历工具,主要用于监听被抓取APP的页面是否有变动,并生成diff报告资料齐全+文档+源
- 基于Webmagic内核的,拓展了用户登录后抓取数据模块资料齐全+文档+源码.zip
- 基于阿里妈妈数据从淘宝抓取商品详情、自动发送淘宝客连接到微博等功能资料齐全+文档+源码.zip
- 基于关键字在主流搜索引擎中抓取信息资料齐全+文档+源码.zip
- 基于安卓模拟器的百度地图热力图自动抓取工具。资料齐全+文档+源码.zip
- 基于股票分析助手,可由于实时抓取市场行情,个股最新业绩预告,业绩快报,业绩报表。以及进行市场综合分析,个股基本面分析。资料齐全+文档+源码.zip
- 基于深度学习的物体识别与抓取方法,六自由度机械臂,python编写程序资料齐全+文档+源码.zip
- 基于摄像头人脸抓取基于tcp协议传数据包给后台,转换成图片后调用百度人脸对比API资料齐全+文档+源码.zip
- 基于浏览器插件的数据抓取工具、做成浏览器插件无需模拟登入,能最真实的模仿用户行为和特征资料齐全+文档+源码.zip
- 基于微信移动端的公众号文章抓取资料齐全+文档+源码.zip
- 基于未知物体三维点云特征的机器人六自由度抓取资料齐全+文档+源码.zip
- 基于油猴的脚本抓取淘宝和天猫店铺的商品信息(以JSON格式保存)资料齐全+文档+源码.zip