Android开发

OkHttp3基础篇:Home

2016-04-04  本文已影响4224人  zhuanghongji

官方用一句话进行了介绍:

An HTTP & HTTP/2 client for Android and Java applications.

一、概述

HTTP 是互联网上应用最为广泛的一种网络协议,可用于传输数据和媒体
高效使用HTTP不仅让内容加载更快,且节省带宽

对HTTP 不熟可以看之前的一篇文章:HTTP,请求消息,响应消息,Cookie(详细总结)

OkHttp支持Android 2.3及以上。对于Java,最低要求是1.7

二、使用例子

1. GET A URL

下面这个程序下载一个URL,并以字符串打印出其内容:

// 关键代码
OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException { 
    Request request = new Request.Builder() 
          .url(url) 
          .build(); 

    Response response = client.newCall(request).execute(); 
    return response.body().string();
}

// 完整代码
package okhttp3.guide;

import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GetExample {
  OkHttpClient client = new OkHttpClient();

  String run(String url) throws IOException {
    Request request = new Request.Builder()
        .url(url)
        .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
  }

  public static void main(String[] args) throws IOException {
    GetExample example = new GetExample();
    String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
    System.out.println(response);
  }
}

2. POST TO A SERVER

提交数据到服务端

// 关键代码
public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

// 完整代码
package okhttp3.guide;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PostExample {
  public static final MediaType JSON
      = MediaType.parse("application/json; charset=utf-8");

  OkHttpClient client = new OkHttpClient();

  String post(String url, String json) throws IOException {
    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
    Response response = client.newCall(request).execute();
    return response.body().string();
  }

  String bowlingJson(String player1, String player2) {
    return "{'winCondition':'HIGH_SCORE',"
        + "'name':'Bowling',"
        + "'round':4,"
        + "'lastSaved':1367702411696,"
        + "'dateStarted':1367702378785,"
        + "'players':["
        + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39},"
        + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}"
        + "]}";
  }

  public static void main(String[] args) throws IOException {
    PostExample example = new PostExample();
    String json = example.bowlingJson("Jesse", "Jake");
    String response = example.post("http://www.roundsapp.com/post", json);
    System.out.println(response);
  }
}

三、ja包下载或引用

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.2.0</version>
</dependency>
    compile 'com.squareup.okhttp3:okhttp:3.2.0'

参考文章:
[1] 官方原文:OkHttp

上一篇下一篇

猜你喜欢

热点阅读