Okhttp使用

注意事项

response.body().string()只可使用一次,使用第二次会报错!!!!

概述

  • 一般的get请求
  • 一般的post请求
  • 基于Http的文件上传
  • 文件下载
  • 加载图片
  • 支持请求回调,直接返回对象、对象集合
  • 支持session的保持

添加okhttp

最简单的方法是在Android Studio 3.1的build.gradle(module.app)中 添加

implementation ‘com.squareup.okhttp3:okhttp:3.10.0’

implementation ‘com.squareup.okio:okio:1.14.1’

然后点击右上角sync now

okhttp GET请求

获取网页的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
//请求的url
.url("https://github.com/hongyangAndroid")
//请求的方式
.get()
//构建请求Request对象
.build();
//new call
Call call = mOkHttpClient.newCacll(request);
//请求加入调度
call.enqueue(new Callback()
{
//请求错误回调方法
@Override
public void onFailure(Request request, IOException e)
{
}
//异步请求(非主线程)
@Override
public void onResponse(final Response response) throws IOException
{
String htmlStr = response.body().string();
}
});

okhttp POST请求(提交表单方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
OkHttpClient okHttpClient  = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();

//post方式提交的数据
RequestBody formBody = new FormBody.Builder()
.add("name", "android基础")
.add("price", "50")
.build();

final Request request = new Request.Builder()
.url("http://172.20.192.168:8080/getbookByFrom")//请求的url
.post(formBody)
.build();


//创建/Call
Call call = okHttpClient.newCall(request);
//加入队列 异步操作
call.enqueue(new Callback() {
//请求错误回调方法
@Override
public void onFailure(Call call, IOException e) {
System.out.println("连接失败");
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code()==200) {
System.out.println(response.body().string());
}
}
});

okhttp POST请求(提交json格式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
OkHttpClient okHttpClient  = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10,TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();

Book book = new Book();
book.setName("android基础");
book.setPrice(59);
//使用Gson 添加 依赖 compile 'implementation 'com.google.code.gson:gson:2.8.4''
Gson gson = new Gson();
//使用Gson将对象转换为json字符串
String json = gson.toJson(book);

//MediaType 设置Content-Type 标头中包含的媒体类型值
RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
, json);

Request request = new Request.Builder()
.url("http://172.20.192.168:8080/getbookByJson")//请求的url
.post(requestBody)
.build();

//创建/Call
Call call = okHttpClient.newCall(request);
//加入队列 异步操作
call.enqueue(new Callback() {
//请求错误回调方法
@Override
public void onFailure(Call call, IOException e) {
System.out.println("连接失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.body().string());
}
});

RequestBody的数据格式都要指定Content-Type,常见的有三种:

  • application/x-www-form-urlencoded 数据是个普通表单
  • multipart/form-data 数据里有文件
  • application/json 数据是个json