关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » 安卓开发 »Android 网络框架 OKHttp 学习

    Android 网络框架 OKHttp 学习

    2015-10-31 00:00:00 出处:JeremyHe
    分享

    OKHttp

    okHttp: OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。假如你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。

    An HTTP & SPDY client for Android and Java applications 从Android4.4开始HttpURLConnection的底层实现采用的是okHttp.

    使用要求:对于Android:2.3以上,对于Java:java7以上 两个模块: okhttp-urlconnection实现.HttpURLConnection API; okhttp-apache实现Apache HttpClient API. 依赖:okio(https://github.com/square/okio): Okio, which OkHttp uses for fast I/O and resizable buffers.

    安装:

    maven:

    com.squareup.okhttpokhttp2.3.0

    Gradle:

    compile 'com.squareup.okhttp:okhttp:2.3.0'

    GET A URL

    同步GET:

      private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        Request request = new Request.Builder()
            .url(http://publicobject.com/helloworld.txt)
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        Headers responseHeaders = response.headers();
        for (int i = 0; i < responseHeaders.size(); i++) {
          System.out.println(responseHeaders.name(i) + :  + responseHeaders.value(i));
        }
    
        System.out.println(response.body().string());
      }

    异步GET:

    在一个工作线程中下载文件,当响应可读时回调Callback接口。读取响应时会阻塞当前线程。OkHttp现阶段不提供异步api来接收响应体。

      private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        Request request = new Request.Builder()
            .url(http://publicobject.com/helloworld.txt)
            .build();
    
        client.newCall(request).enqueue(new Callback() {
          @Override public void onFailure(Request request, Throwable throwable) {
            throwable.printStackTrace();
          }
    
          @Override public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
            Headers responseHeaders = response.headers();
            for (int i = 0; i < responseHeaders.size(); i++) {
              System.out.println(responseHeaders.name(i) + :  + responseHeaders.value(i));
            }
    
            System.out.println(response.body().string());
          }
        });
      }

    访问Header:

      private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        Request request = new Request.Builder()
            .url(https://api.github.com/repos/square/okhttp/issues)
            .header(User-Agent, OkHttp Headers.java)
            .addHeader(Accept, application/json; q=0.5)
            .addHeader(Accept, application/vnd.github.v3+json)
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        System.out.println(Server:  + response.header(Server));
        System.out.println(Date:  + response.header(Date));
        System.out.println(Vary:  + response.headers(Vary));
      }

    POST TO A SERVER

    Posting a String:

    public static final MediaType jsonReq
        = MediaType.parse(application/json; charset=utf-8);
    
    OkHttpClient client = new OkHttpClient();
    
    String post(String url, String json) throws IOException {
      RequestBody body = RequestBody.create(jsonReq, json);
      Request request = new Request.Builder()
          .url(url)
          .post(body)
          .build();
      Response response = client.newCall(request).execute();
      return response.body().string();
    }

    Posting Streaming:

     public static final MediaType MEDIA_TYPE_MARKDOWN
          = MediaType.parse(text/x-markdown; charset=utf-8);
    
      private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        RequestBody requestBody = new RequestBody() {
          @Override public MediaType contentType() {
            return MEDIA_TYPE_MARKDOWN;
          }
    
          @Override public void writeTo(BufferedSink sink) throws IOException {
            sink.writeUtf8(Numbers
    );
            sink.writeUtf8(-------
    );
            for (int i = 2; i <= 997; i++) {
              sink.writeUtf8(String.format( * %s = %s
    , i, factor(i)));
            }
          }
    
          private String factor(int n) {
            for (int i = 2; i < n; i++) {
              int x = n / i;
              if (x * i == n) return factor(x) +  ×  + i;
            }
            return Integer.toString(n);
          }
        };
    
        Request request = new Request.Builder()
            .url(https://api.github.com/markdown/raw)
            .post(requestBody)
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        System.out.println(response.body().string());
      }

    Posting a File:

     public static final MediaType MEDIA_TYPE_MARKDOWN
          = MediaType.parse(text/x-markdown; charset=utf-8);
    
      private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        File file = new File(README.md);
    
        Request request = new Request.Builder()
            .url(https://api.github.com/markdown/raw)
            .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        System.out.println(response.body().string());
      }

    Posting from parameters:

    private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        RequestBody formBody = new FormEncodingBuilder()
            .add(search, Jurassic Park)
            .build();
        Request request = new Request.Builder()
            .url(https://en.wikipedia.org/w/index.php)
            .post(formBody)
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        System.out.println(response.body().string());
      }

    Posting a multipart request:

     private static final String IMGUR_CLIENT_ID = ...;
      private static final MediaType MEDIA_TYPE_PNG = MediaType.parse(image/png);
    
      private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
        RequestBody requestBody = new MultipartBuilder()
            .type(MultipartBuilder.FORM)
            .addPart(
                Headers.of(Content-Disposition, form-data; name=	itle),
                RequestBody.create(null, Square Logo))
            .addPart(
                Headers.of(Content-Disposition, form-data; name=image),
                RequestBody.create(MEDIA_TYPE_PNG, new File(website/static/logo-square.png)))
            .build();
    
        Request request = new Request.Builder()
            .header(Authorization, Client-ID  + IMGUR_CLIENT_ID)
            .url(https://api.imgur.com/3/image)
            .post(requestBody)
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        System.out.println(response.body().string());
      }

    Posing Json with Gson

     private final OkHttpClient client = new OkHttpClient();
      private final Gson gson = new Gson();
    
      public void run() throws Exception {
        Request request = new Request.Builder()
            .url(https://api.github.com/gists/c2a7c39532239ff261be)
            .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        Gist gist = gson.fromJson(response.body().charStream(), Gist.class);
        for (Map.Entry entry : gist.files.entrySet()) {
          System.out.println(entry.getKey());
          System.out.println(entry.getValue().content);
        }
      }
    
      static class Gist {
        Map files;
      }
    
      static class GistFile {
        String content;
      }

    Response Caching:

    为了缓存响应,你需要一个你可以读写的缓存目录,和缓存大小的限制。这个缓存目录应该是私有的,不信任的程序应不能读取缓存内容。

    一个缓存目录同时拥有多个缓存访问是错误的。大多数程序只需要调用一次 new OkHttp() ,在第一次调用时配置好缓存,然后其他地方只需要调用这个实例就可以了。否则两个缓存示例互相干扰,破坏响应缓存,而且有可能会导致程序崩溃。

    响应缓存使用HTTP头作为配置。你可以在请求头中添加 Cache-Control: max-stale=3600 ,OkHttp缓存会支持。你的服务通过响应头确定响应缓存多长时间,例如使用 Cache-Control: max-age=9600 。

    private final OkHttpClient client;
    
      public CacheResponse(File cacheDirectory) throws Exception {
        int cacheSize = 10 * 1024 * 1024; // 10 MiB
        Cache cache = new Cache(cacheDirectory, cacheSize);
    
        client = new OkHttpClient();
        client.setCache(cache);
      }
    
      public void run() throws Exception {
        Request request = new Request.Builder()
            .url(http://publicobject.com/helloworld.txt)
            .build();
    
        Response response1 = client.newCall(request).execute();
        if (!response1.isSuccessful()) throw new IOException(Unexpected code  + response1);
    
        String response1Body = response1.body().string();
        System.out.println(Response 1 response:           + response1);
        System.out.println(Response 1 cache response:     + response1.cacheResponse());
        System.out.println(Response 1 network response:   + response1.networkResponse());
      }

    Canceling a Call

        final Call call = client.newCall(request);
            call.cancel();

    Timeouts:

    private final OkHttpClient client;
    
      public ConfigureTimeouts() throws Exception {
        client = new OkHttpClient();
        client.setConnectTimeout(10, TimeUnit.SECONDS);
        client.setWriteTimeout(10, TimeUnit.SECONDS);
        client.setReadTimeout(30, TimeUnit.SECONDS);
      }

    Handling Authentication:

    private final OkHttpClient client = new OkHttpClient();
    
      public void run() throws Exception {
        client.setAuthenticator(new Authenticator() {
          @Override public Request authenticate(Proxy proxy, Response response) {
            System.out.println(Authenticating for response:  + response);
            System.out.println(Challenges:  + response.challenges());
            String credential = Credentials.basic(jesse, password1);
            return response.request().newBuilder()
                .header(Authorization, credential)
                .build();
          }
    
          @Override public Request authenticateProxy(Proxy proxy, Response response) {
            return null; // Null indicates no attempt to authenticate.
          }
        });
    
        Request request = new Request.Builder()
            .url(http://publicobject.com/secrets/hellosecret.txt)
            .build();
    
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException(Unexpected code  + response);
    
        System.out.println(response.body().string());
      }

    为避免当验证失败时多次重试,我们可以通过返回null来放弃验证:

     if (responseCount(response) >= 3) {
        return null; // If we've failed 3 times, give up.
      }
    //添加以下方法
    private int responseCount(Response response) {
        int result = 1;
        while ((response = response.priorResponse()) != null) {
          result++;
        }
        return result;
      }

    Interceptors

    class LoggingInterceptor implements Interceptor {
      @Override public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
    
        long t1 = System.nanoTime();
        logger.info(String.format(Sending request %s on %s%n%s,
            request.url(), chain.connection(), request.headers()));
    
        Response response = chain.proceed(request);
    
        long t2 = System.nanoTime();
        logger.info(String.format(Received response for %s in %.1fms%n%s,
            response.request().url(), (t2 - t1) / 1e6d, response.headers()));
    
        return response;
      }
    }

    Application Interceptors:

    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(new LoggingInterceptor());

    Network Interceptors

    OkHttpClient client = new OkHttpClient();
    client.networkInterceptors().add(new LoggingInterceptor());
    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    抖音安全与信任开放日:揭秘推荐算法,告别单一标签依赖

    ultraedit编辑器打开文件时,总是提示是否转换为DOS格式,如何关闭?

    Cornell大神Kleinberg的经典教材《算法设计》是最好入门的算法教材

    从 Microsoft 下载中心安装 Windows 7 SP1 和 Windows Server 2008 R2 SP1 之前要执行的步骤

    Llama 2基于UCloud UK8S的创新应用

    火山引擎DataTester:如何使用A/B测试优化全域营销效果

    腾讯云、移动云继阿里云降价后宣布大幅度降价

    字节跳动数据平台论文被ICDE2023国际顶会收录,将通过火山引擎开放相关成果

    这个话题被围观超10000次,火山引擎VeDI如此解答

    误删库怎么办?火山引擎DataLeap“3招”守护数据安全

    IT头条

    平替CUDA!摩尔线程发布MUSA 4性能分析工具

    00:43

    三起案件揭开侵犯个人信息犯罪的黑灰产业链

    13:59

    百度三年开放2.1万实习岗,全力培育AI领域未来领袖

    00:36

    工信部:一季度,电信业务总量同比增长7.7%,业务收入累计完成4469亿元

    23:42

    Gartner:2024年全球半导体营收6559亿美元,AI助力英伟达首登榜首

    18:04

    技术热点

    iOS 8 中如何集成 Touch ID 功能

    windows7系统中鼠标滑轮键(中键)的快捷应用

    MySQL数据库的23个特别注意的安全事项

    Kruskal 最小生成树算法

    Ubuntu 14.10上安装新的字体图文教程

    Ubuntu14更新后无法进入系统卡在光标界面解怎么办?

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。