Giter VIP home page Giter VIP logo

ahttptool's Introduction

Httpclient

android 6.0 移除了 apache httpclient

为了做兼容,我们必须考虑这个问题,下面推荐几个解决方案

1.使用okhttpvolley

2.官网下载apache httpclient jar包

3.使用HttpURLConnection访问网络。

##这个库是基于HttpURLConnection封装的同步和异步网络请求库。

##异步请求

 AsyncHttpCliect cliect = new AsyncHttpCliect();
        Map<String, String> map = new HashMap<String, String>();
        map.put("ip", "63.223.108.42");
        cliect.post("http://ip.taobao.com/service/getIpInfo.php", map, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(HttpResponse response) {


            }
        });
        cliect.get("http://www.baidu.com", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(HttpResponse response) {

            }
        });

同步访问如下: ##同步请求

 HttpClient httpClient=new HttpClient();
        try {
            httpClient.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");
            httpClient.get("http://www.baidu.com").addParam("name","sunger").addParam("age","22").execute();
        } catch (HttpClientException e) {
            e.printStackTrace();
        }

##文件下载,支持断点续传

 DownloadDelegate   downloadDelegate = new DownloadDelegate(this, "http://dldir1.qq.com/weixin/android/weixin625android620.apk", "/sdcard/a/", new DownloadHandler() {
            @Override
            protected void onFinish(String msg) {

            }

            @Override
            public void onError(String msg) {

            }

            @Override
            protected void onSpeed(String speed) {

            }
        });
        //开始下载
        downloadDelegate.start();
         //暂停下载
        downloadDelegate.pause();

##Https请求导入证书

  AsyncHttpCliect cliect = new AsyncHttpCliect();
        try {
            cliect.setCertificates(getAssets().open("srca.cer"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        cliect.get("https://kyfw.12306.cn/otn/", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(HttpResponse response) {
                Log.d("-----HttpResponse--", response.getStatusCode() + "");
            }

            @Override
            public void onFailure(Throwable error) {
                Log.d("-----Error--", error.getMessage());
            }
        });

下一次更新将实现HttpResponseCacheHttp缓存功能

ahttptool's People

Watchers

 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.