Code Monkey home page Code Monkey logo

httputils's Introduction

HttpUtils

单文件Http工具类,支持GET,POST,断点续传,多线程断点续传

1普通请求

        String baiduUrl = "http://www.baidu.com";
        String response = HttpUtils.request(baiduUrl).get();
        Log.d("HttpUtils", baiduUrl);
        Log.d("HttpUtils", String.valueOf(response));

2带参数

API Store中免费接口

        String apikey = "7c835ed01bd98ae64d1ff44d7a09b8e3";
        String httpUrl = "http://apis.baidu.com/heweather/weather/free";
        String response = HttpUtils.request(httpUrl)
                .param("city", "beijing")
                .property("apikey", apikey)
                .get();
        Log.d("HttpUtils", httpUrl);
        Log.d("HttpUtils", String.valueOf(response));

3普通下载

        File sdcard = Environment.getExternalStorageDirectory();
        final String meizi1 = "http://i.meizitu.net/2014/09/23mt01.jpg";
        File file1 = new File(sdcard, "meizi1.jpg");
        boolean r = HttpUtils.request(meizi1)
                .download()
                .target(file1)
                .downloadInOneUnit();
        Log.d("HttpUtils", meizi1);
        Log.d("HttpUtils", String.valueOf(r));

4断点续传

        String meizi2 = "http://i.meizitu.net/2014/09/23mt02.jpg";
        File file2 = new File(sdcard, "meizi2.jpg");
        boolean r = HttpUtils.request(meizi1)
                .download()
                .target(file2)
                .breakpoint(true)
                .downloadInOneUnit();
        Log.d("HttpUtils", meizi2);
        Log.d("HttpUtils", String.valueOf(r));

5多线程断点续传

        final String meizi3 = "http://i.meizitu.net/2013/07/2011103023431314867.jpg";
        final File file3 = new File(sdcard, "meizi3.jpg");
        HttpUtils.DownloadUnit[] dus = HttpUtils.request(meizi3)
                .download()
                .target(file3)
                .breakpoint(true)
                .units(3);
        if(dus != null) {
            for (final HttpUtils.DownloadUnit du : dus) {
                Log.d("HttpUtils", String.valueOf(du.id));
                Log.d("HttpUtils", String.valueOf(du.startBytes));
                Log.d("HttpUtils", String.valueOf(du.endBytes));
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean r = HttpUtils.request(meizi1)
                                .download()
                                .target(file3)
                                .breakpoint(true)
                                .downloadUnit(du);
                        Log.d("HttpUtils", meizi3);
                        Log.d("HttpUtils", String.valueOf(r));
                    }
                }).start();
            }
        }

httputils's People

Contributors

laole918 avatar

Watchers

James Cloos avatar imchen avatar

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.