zoukankan      html  css  js  c++  java
  • Okhttp

    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);
    }

    });



     
  • 相关阅读:
    二分法查找
    重构方法之一:提炼方法(Extract Method)读书笔记
    使用SQL命令手动写入Discuz帖子内容
    调整linux系统时间和时区
    怎样给访问量过大的mysql数据库减压
    MySQL提示“too many connections”的解决办法
    CentOS 6安装php加速软件Zend Guard
    CmsTop 大众版运行环境搭建 (CentOS+Nginx+PHP FastCGI)
    LEMP构建高性能WEB服务器(CentOS+Nginx+PHP+MySQL)
    CentOS-6.3安装配置Nginx
  • 原文地址:https://www.cnblogs.com/huihuizhang/p/7593031.html
Copyright © 2011-2022 走看看