OkHttp 简易封装工具 FastHttpClient

码农软件 · 软件分类 · 网络工具包 · 2019-02-25 07:13:19

软件介绍

FastHttpClient 是简易封装 OkHttp 的工具(需要jdk8以上),高效使用http(s) post get 。

用法

1.同步的get

FastHttpClient.get().
        url(url).
        addParams("userName", "icecool").
        addParams("password", "111111").
        build().
        execute();

2.同步的post

FastHttpClient.post().
        url(url).
        addParams("userName", "icecool").
        addParams("password", "111111").
        build().
        execute();

3.异步的get

FastHttpClient.get().
            url(url).
            addParams("userName","icecool").
            addParams("password", "111111").
            build().
            executeAsync(new Callback() {
            @Override
            public void onFailure(Call call, Exception e, int id) {
                //TODO
            }

            @Override
            public void onResponse(Call call,Response response, int id) {
                try {
                    System.out.println(response.body().string());
                } catch (IOException e) {
                }
            }
        });

4.异步的post

FastHttpClient.post().
            url(url).
            addParams("userName","icecool").
            addParams("password", "111111").
            build().
            executeAsync(new Callback() {
            @Override
            public void onFailure(Call call, Exception e, int id) {
                //TODO
            }

            @Override
            public void onResponse(Call call,Response response, int id) {
                try {
                    System.out.println(response.body().string());
                } catch (IOException e) {
                }
            }
        });

5.异步下载文件

FastHttpClient.get().
        url("http://e.hiphotos.baidu.com/image/pic/item/faedab64034f78f0b31a05a671310a55b3191c55.jpg").
        build().addNetworkInterceptor(new DownloadFileInterceptor(){
            @Override
            public void updateProgress(long downloadLenth, long totalLength, boolean isFinish) {
                System.out.println("updateProgress downloadLenth:"+downloadLenth+
                        ",totalLength:"+totalLength+",isFinish:"+isFinish);
            }
        }).
        executeAsync(new DownloadFileCallback("/tmp/tmp.jpg") {//save file to /tmp/tmp.jpg
                @Override
                public void onFailure(Call call, Exception e, int id) {
                    e.printStackTrace();
                }
                @Override
                public void onSuccess(Call call, File file, int id) {
                    super.onSuccess(call, file, id);
                    System.out.println("filePath:"+file.getAbsolutePath());
                }
                @Override
                public void onSuccess(Call call, InputStream fileStream, int id) {
                    System.out.println("onSuccessWithInputStream");
                }
        });
Thread.sleep(50000);

6.上传文件

byte[] imageContent=FileUtil.getBytes("/tmp/test.png");
        response = FastHttpClient.post().
                url(url).
                addFile("file1", "a.txt", "123").
                addFile("file2", "b.jpg", imageContent).
                build().
                connTimeOut(10000).
                execute();
System.out.println(response.body().string());

7.https get

Response response = FastHttpClient.get().url("https://kyfw.12306.cn/otn/").
                build()
                .execute();
System.out.println(response.body().string());

8.https post

Response response = FastHttpClient.post().url("https://kyfw.12306.cn/otn/").
                build()
                .execute();
System.out.println(response.body().string());

本文地址:https://codercto.com/soft/d/77.html

数据结构、算法与应用(原书第2版)

数据结构、算法与应用(原书第2版)

Sartaj Sahni / 王立柱、刘志红 / 机械工业出版社 / 2015-4 / 79.00元

《数据结构、算法与应用——C++语言描述》是享有盛誉的数据结构教科书的第2版。它完整地包含了基本数据结构的内容,是CS2课程的理想用书。作者Sartaj Sahni通过循循善诱的讲解、直观具体的讨论和基于现实的应用,让读者轻松、愉快地学习。新版书着重利用标准模板库(STL),把书中开发的数据结构和算法与相应的STL实现方法相互关联。本书还增加了很多新的实例和练习题。 书中的应用实例是它的特色......一起来看看 《数据结构、算法与应用(原书第2版)》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具