Android okhttp网络框架

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

完整开源地址:https://docs.qq.com/doc/DSkNLaERkbnFoS0ZF

/**

 * 设置请求超时

 */

private static void initOkHttpClient() {

    File sdcache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cache");

    int cacheSize = 10 * 1024 * 1024;

    OkHttpClient.Builder builder = new OkHttpClient.Builder()

            .connectTimeout(15, TimeUnit.SECONDS)

            .writeTimeout(20, TimeUnit.SECONDS)

            .readTimeout(20, TimeUnit.SECONDS)

            .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize));

    mOkHttpClient = builder.build();

}



/**

 * get异步请求

 */

public void getAsynHttp(final int what, final HttpCallBack httpCallBack, final String httpUrl) {

    Request.Builder requestBuilder = new Request.Builder().url(httpUrl);

    requestBuilder.method("GET", null);

    Request request = requestBuilder.build();

    Call mcall = mOkHttpClient.newCall(request);

    mcall.enqueue(new Callback() {

        @Override

        public void onFailure(Call call, IOException e) {

            httpCallBack.onFailure(what, e.toString());

        }



        @Override

        public void onResponse(Call call, Response response) throws IOException {

            httpCallBack.onResponse(what, response.body().string());

        }

    });

}



/**

 * post异步请求

 */

public void postAsynHttp(final int what, final HttpCallBack httpCallBack, final String httpUrl, final Map<String, String> stringMap) {

    FormBody.Builder formBody = new FormBody.Builder();

    for (String key : stringMap.keySet()) {

        formBody.add(key, stringMap.get(key));

    }

    Request request = new Request.Builder()

            .url(httpUrl)

            .post(formBody.build())

            .build();

    Call call = mOkHttpClient.newCall(request);

    call.enqueue(new Callback() {

        @Override

        public void onFailure(Call call, IOException e) {

            httpCallBack.onFailure(what, e.toString());

        }



        @Override

        public void onResponse(Call call, Response response) throws IOException {

            httpCallBack.onResponse(what, response.body().string());

        }

    });

}



/**

 * 异步下载文件

 */

public void downAsynFile(final int what, final HttpCallBack httpCallBack, final String httpFilePath, final String filePath) {

    Request request = new Request.Builder().url(httpFilePath).build();

    mOkHttpClient.newCall(request).enqueue(new Callback() {

        @Override

        public void onFailure(Call call, IOException e) {

            httpCallBack.onFailure(what, e.toString());

        }



        @Override

        public void onResponse(Call call, Response response) {

            InputStream inputStream = response.body().byteStream();

            FileOutputStream fileOutputStream = null;

            try {

                fileOutputStream = new FileOutputStream(new File(filePath));

                byte[] buffer = new byte[5 * 1024 * 1024];

                int len = 0;

                while ((len = inputStream.read(buffer)) != -1) {

                    fileOutputStream.write(buffer, 0, len);

                }

                fileOutputStream.flush();

                httpCallBack.onResponse(what, "下载成功");

            } catch (IOException e) {

                e.printStackTrace();

                httpCallBack.onResponse(what, "下载异常:" + e.toString());

            }

        }

    });

}



public interface HttpCallBack {



    /**

     * 访问成功回调接口

     */

    void onResponse(int what, String response);



    /**

     * 访问成错误回调接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值