大家好,又见面了,我是你们的朋友全栈君。
Java OkHttp使用
本文使用eclipse编辑器,gradle依赖jar,如若未配置此环境,请转Java Eclipse配置gradle编译项目配置好环境后再查看此文
- 在build.gradle中添加依赖
compile 'com.squareup.okhttp3:okhttp:3.8.1'
- 同步Get请求
-
/**
-
* 同步get请求
-
*/
-
public static void syncGet() throws Exception{
-
String urlBaidu =
"http://www.baidu.com/";
-
OkHttpClient okHttpClient =
new OkHttpClient();
// 创建OkHttpClient对象
-
Request request =
new Request.Builder().url(urlBaidu).build();
// 创建一个请求
-
Response response = okHttpClient.newCall(request).execute();
// 返回实体
-
if (response.isSuccessful()) {
// 判断是否成功
-
/**获取返回的数据,可通过response.body().string()获取,默认返回的是utf-8格式;
-
* string()适用于获取小数据信息,如果返回的数据超过1M,建议使用stream()获取返回的数据,
-
* 因为string() 方法会将整个文档加载到内存中。*/
-
System.
out.println(response.body().
string());
// 打印数据
-
}
else {
-
System.
out.println(
"失败");
// 链接失败
-
}
-
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 异步Get请求
-
/**
-
* 异步Get请求
-
*/
-
public static void asyncGet() {
-
String urlBaidu =
"http://www.baidu.com/";
-
OkHttpClient okHttpClient =
new OkHttpClient();
// 创建OkHttpClient对象
-
Request request =
new Request.Builder().url(urlBaidu).build();
// 创建一个请求
-
okHttpClient.newCall(request).enqueue(
new Callback() {
// 回调
-
-
public void onResponse(Call call, Response response) throws IOException {
-
// 请求成功调用,该回调在子线程
-
System.
out.println(response.body().
string());
-
}
-
-
public void onFailure(Call call, IOException e) {
-
// 请求失败调用
-
System.
out.println(e.getMessage());
-
}
-
});
-
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
4.Post提交表单
-
/**
-
* Post提交表单
-
*/
-
public static void postFromParameters() {
-
String url =
"http://v.juhe.cn/wepiao/query";
// 请求链接
-
String KEY =
"9488373060c8483a3ef6333353fdc7fe";
// 请求参数
-
OkHttpClient okHttpClient =
new OkHttpClient();
// OkHttpClient对象
-
RequestBody formBody =
new FormBody.Builder().
add(
"key", KEY).build();
// 表单键值对
-
Request request =
new Request.Builder().url(url).post(formBody).build();
// 请求
-
okHttpClient.newCall(request).enqueue(
new Callback() {
// 回调
-
-
public void onResponse(Call call, Response response) throws IOException {
-
System.
out.println(response.body().
string());
//成功后的回调
-
}
-
-
public void onFailure(Call call, IOException e) {
-
System.
out.println(e.getMessage());
//失败后的回调
-
}
-
});
-
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- Post提交字符串
- /**
- * Post提交字符串
- * 使用Post方法发送一串字符串,但不建议发送超过1M的文本信息
- */
- public static void postStringParameters(){
- MediaType MEDIA_TYPE = MediaType.parse( "text/text; charset=utf-8");
- String url = "http://v.juhe.cn/wepiao/query"; // 请求链接
- OkHttpClient okHttpClient = new OkHttpClient(); // OkHttpClient对象
- String string = "key=9488373060c8483a3ef6333353fdc7fe"; // 要发送的字符串
- /**
- * RequestBody.create(MEDIA_TYPE, string)
- * 第二个参数可以分别为:byte[],byteString,File,String。
- */
- Request request = new Request.Builder().url(url)
- .post(RequestBody.create(MEDIA_TYPE, string)).build();
- okHttpClient.newCall(request).enqueue( new Callback() {
- public void onResponse(Call call, Response response) throws IOException {
- System. out.println(response.body(). string());
- }
-
- public void onFailure(Call call, IOException e) {
- System. out.println(e.getMessage());
- }
- });
- }
- 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
- Gson解析Response的Gson对象
- /**
- * Gson解析Response的Gson对象
- */
- public static void gsonResponsePost() {
- String url = "http://v.juhe.cn/wepiao/query"; // 请求链接
- String KEY = "9488373060c8483a3ef6333353fdc7fe"; // 请求参数
- OkHttpClient okHttpClient = new OkHttpClient(); // OkHttpClient对象
- RequestBody formBody = new FormBody.Builder(). add( "key", KEY).build(); // 表单键值对
- Request request = new Request.Builder().url(url).post(formBody).build(); // 请求
- okHttpClient.newCall(request).enqueue( new Callback() { // 回调
-
- public void onResponse(Call call, Response response) throws IOException {
- //成功后的回调
- Gson gson = new Gson();
- Info info = gson.fromJson(response.body(). string(), Info.class);
- System. out.println(info.toString());
- }
-
- public void onFailure(Call call, IOException e) {
- System. out.println(e.getMessage()); //失败后的回调
- }
- });
- }
- /**
- * Java Bean
- */
- public class Info{
- int error_code; //状态码
- String reason; // 返回状态文字
- Result result; // 页面URL
- @ Override
- public String toString( ) {
- return "Info [error_code=" + error_code + ", reason=" + reason + ", result=" + result.toString() + "]";
- }
- }
- /**
- * Java Bean
- */
- public class Result{
- String h5url;
- String h5weixin;
- @ Override
- public String toString( ) {
- return "Result [h5url=" + h5url + ", h5weixin=" + h5weixin + "]";
- }
- }
- 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
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- okhttp3 设置超时时间
- /**
- * 设置超时
- * @throws IOException
- */
- public static void timeOutPost() throws IOException {
- OkHttpClient client = new OkHttpClient.Builder()
- .connectTimeout( 10, TimeUnit.SECONDS) //设置链接超时
- .writeTimeout( 10, TimeUnit.SECONDS) // 设置写数据超时
- .readTimeout( 30, TimeUnit.SECONDS) // 设置读数据超时
- .build();
- Request request = new Request.Builder().url( "http://www.baidu.com/").build();
-
- Response response = client.newCall(request).execute();
- System.out.println( "Response completed: " + response);
- }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 缓存设置
- /**
- * 缓存设置
- * @throws Exception
- */
- public static void cachePost() throws Exception {
- File sdcache = new File( "D:/file");
- int cacheSize = 10 * 1024 * 1024; // 10 MiB
- OkHttpClient client = new OkHttpClient()
- .Builder()
- .cache( new Cache(sdcache.getAbsoluteFile(), cacheSize))
- .build();
- 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());
-
- request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
- Response response2 = client.newCall(request).execute();
- if (!response2.isSuccessful()) throw new IOException( "Unexpected code " + response2);
-
- String response2Body = response2.body(). string();
- System. out.println( "Response 2 response: " + response2);
- System. out.println( "Response 2 cache response: " + response2.cacheResponse());
- System. out.println( "Response 2 network response: " + response2.networkResponse());
-
- System. out.println( "Response 2 equals Response 1? " + response1Body. equals(response2Body));
- }
- 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
打印结果:
- Response 1 response: Response{protocol=http/ 1.1, code= 200, message=OK, url=https: //publicobject.com/helloworld.txt}
- Response 1 cache response: Response{protocol=http/ 1.1, code= 200, message=OK, url=https: //publicobject.com/helloworld.txt}
- Response 1 network response: null
- Response 2 response: Response{protocol=http/ 1.1, code= 200, message=OK, url=https: //publicobject.com/helloworld.txt}
- Response 2 cache response: null
- Response 2 network response: Response{protocol=http/ 1.1, code= 200, message=OK, url=https: //publicobject.com/helloworld.txt}
- Response 2 equals Response 1? true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 复用OkHttpClient
- /**
- * 所有HTTP请求的代理设置,超时,缓存设置等都需要在OkHttpClient中设置。 如果需要更改一个请求的配置,可以使用
- * OkHttpClient.newBuilder()获取一个builder对象,
- * 该builder对象与原来OkHttpClient共享相同的连接池,配置等。
- */
- public static void shareClient() {
- OkHttpClient client = new OkHttpClient();
- Request request = new Request.Builder().url( "http://www.baidu.com/").build();
-
- try {
- // Copy to customize OkHttp for this request.
- OkHttpClient copy = client.newBuilder().readTimeout( 500, TimeUnit.MILLISECONDS).build();
- Response response = copy.newCall(request).execute();
- System. out.println( "Response 1 succeeded: " + response);
- } catch (IOException e) {
- System. out.println( "Response 1 failed: " + e);
- }
-
- try {
- // Copy to customize OkHttp for this request.
- OkHttpClient copy = client.newBuilder().readTimeout( 3000, TimeUnit.MILLISECONDS).build();
- Response response = copy.newCall(request).execute();
- System. out.println( "Response 2 succeeded: " + response);
- } catch (IOException e) {
- System. out.println( "Response 2 failed: " + e);
- }
- }
- 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
- OkHttp3处理验证
- /**
- * 登录验证
- * @throws IOException
- */
- public static void authenticatorPost() throws IOException {
- OkHttpClient okHttpClient =
- new OkHttpClient
- .Builder()
- .authenticator( new Authenticator() {
-
- public Request authenticate(Route route, Response response) throws IOException {
- System.out.println( response.challenges().toString());
- String credential = Credentials.basic( "jesse", "password1");
- return response
- . request()
- .newBuilder()
- .addHeader( "Authorization", credential)
- .build();
- }
- })
- .build();
- Request request = new Request.Builder().url( "http://publicobject.com/secrets/hellosecret.txt").build();
- Response response = okHttpClient.newCall( request). execute();
- if (! response.isSuccessful()) throw new IOException( "Unexpected code " + response);
-
- System.out.println( response.body(). string());
- }
- 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
</div>
<!-- --></div> </div>
</div>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/126773.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...