0%

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
package com.star;

import okhttp3.*;

import java.io.IOException;

public class RequsetTest {
public static void main(String[] args) throws IOException {
OkHttpClient client = new OkHttpClient.Builder().build();
final Request request = new Request.Builder().url("https://github.com/").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Fail");
}

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

}
}

上述程序优先是 1 输出,然后,才是网页信息。

请我喝杯咖啡吧~