Git Product home page Git Product logo

okhttputils's Introduction

OkHttpUtils

最简单的okhttp封装,CallBack方法执行在UI线程。支持get请求,post请求,支持文件上传和下载。

使用方法:

代码很简单,只有三个Java文件,建议下载后将Java文件拷贝到工程中使用。

封装的功能有:

一般的get请求
一般的post请求
一般的put请求
一般的delete请求
上传单个文件(包含进度)
上传list集合文件
上传map集合文件
文件下载(包含进度)
图片下载(实现了图片的压缩)

使用示例

GET请求

String url = "https://www.baidu.com/";
OkhttpUtil.okHttpGet(url, new CallBackUtil.CallBackString() {
    @Override
    public void onFailure(Call call, Exception e) {}

    @Override
    public void onResponse(String response) {
        Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_SHORT).show();
        Log.d("kwwl",response);
    }
});

POST请求

String url = "https://www.baidu.com/";
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("title","title");
OkhttpUtil.okHttpPost(url, paramsMap, new CallBackUtil.CallBackString() {
    @Override
    public void onFailure(Call call, Exception e) {

    }

    @Override
    public void onResponse(String response) {

    }
});

PUT请求

String url = "https://www.baidu.com/";
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("title","title");
OkhttpUtil.okHttpPut(url, paramsMap, new CallBackUtil.CallBackString() {
    @Override
    public void onFailure(Call call, Exception e) {

    }

    @Override
    public void onResponse(String response) {

    }
});

DELETE请求

String url = "https://www.baidu.com/";
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("title","title");
OkhttpUtil.okHttpDelete(url, paramsMap, new CallBackUtil.CallBackString() {
    @Override
    public void onFailure(Call call, Exception e) {

    }

    @Override
    public void onResponse(String response) {

    }
});

上传文件

File file = new File(Environment.getExternalStorageDirectory()+"/kwwl/abc.jpg");
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("title","title");
OkhttpUtil.okHttpUploadFile(url, file, "image", OkhttpUtil.FILE_TYPE_IMAGE, paramsMap, new CallBackUtil.CallBackString() {
    @Override
    public void onFailure(Call call, Exception e) {

    }

    @Override
    public void onProgress(float progress, long total) {

    }

    @Override
    public void onResponse(String response) {

    }
});

下载文件

OkhttpUtil.okHttpDownloadFile("url", new CallBackUtil.CallBackFile("fileDir","fileName") {
    @Override
    public void onFailure(Call call, Exception e) {

    }

    @Override
    public void onProgress(float progress, long total) {
    }

    @Override
    public void onResponse(File response) {

    }
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.