码迷,mamicode.com
首页 > Web开发 > 详细

Okhttp

时间:2017-09-25 19:08:48      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:make   callback   init   param   exec   tcl   over   exce   logger   

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

标签:make   callback   init   param   exec   tcl   over   exce   logger   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!