7

Android网络请求(3) 网络请求框架OkHttp - 高同学,你好

 1 year ago
source link: https://www.cnblogs.com/nhgtx/p/16916050.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Android网络请求(3) 网络请求框架OkHttp

本节我们来讲解OkHtpp网络请求框架

什么是网络请求框架

在我的理解中,网络请求框架是为了方便我们更加便捷规范的进行网络请求所建的类,我们通过调用该类中的方法可以快速地进行网络请求,切帮我们封装好了一些数据的处理方法,方便我们用更少的代码去做更多的事。

Android常见的网络请求框架

在android开发中,除了java原生的HttpURLConnection,常见的网络请求框架有

OkHttp

Volley

Retrofit

HttpClient

这其中最好用的莫过于Retrofit了,但是Retrofit是在OkHttp基础上再次进行封装的,所以我们先了解OkHttp网络请求,这对于我们日后的安卓开发也是有着很大的帮助。

OkHttp进行四种请求

在项目文件下build.gradle(app)的dependencies中导入所需要的库

implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.12.0'

这里我们使用3.12.0版本,如果需要使用其他版本,修改version版本号为自己想要的版本。

创建四种方法

public static String get(String src){// 实例化OkHttpClient OkHttpClient okHttpClient = new OkHttpClient();// 构建request请求 Request request = new Request.Builder() .url(src) .get() .build();// 发送请求获取返回数据 Call call = okHttpClient.newCall(request);// 获取响应体 ResponseBody body = null; String string = null; try { body = call.execute().body();// 转换成string string = body.string(); } catch (IOException e) { e.printStackTrace(); } return string; }

可以看出这些代码对比起java原生的HttpURLConnection请求来说,简短了很多,不再需要设置一些超时,IO流之类的操着,那么因为OkHttp封装时给定了默认值,这样就大大减少了我们的工作量,我们现在只需要发送请求获取响应数据就行了。

public static String post(String src,String json){// 实例化OkHttpClient OkHttpClient okHttpClient = new OkHttpClient();// 构建请求体 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),json);// 构建request请求 Request request = new Request.Builder() .post(requestBody) .url(src) .build();// 发送请求获取返回数据 Call call = okHttpClient.newCall(request);// 获取响应体 ResponseBody body = null; String string = null; try { body = call.execute().body();// 转换成string string = body.string(); } catch (IOException e) { e.printStackTrace(); } return string; }

可以看出,这里我们只是修改了.get为.post并且传入了请求体,等于只需要新构建一个请求体,将我们的要向服务端发送的数据加入其中即可,这里我们使用的时application/json的方式,其他方式在第二篇提过一下,感兴趣的可以自己查阅资料。

put请求就更简单了,在原post请求中修改.post为.put即可,没有任何难度

public static String put(String src,String json){// 实例化OkHttpClient OkHttpClient okHttpClient = new OkHttpClient();// 构建请求体 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),json);// 构建request请求 Request request = new Request.Builder() .url(src) .put(requestBody) .build();// 发送请求获取返回数据 Call call = okHttpClient.newCall(request);// 获取响应体 ResponseBody body = null; String string = null; try { body = call.execute().body();// 转换成string string = body.string(); } catch (IOException e) { e.printStackTrace(); } return string; }

DELETE

delete请求有两种不同的方式:携带参数与不携带参数。

携带参数:携带参数实在原post请求中将.post修改为.delete,一样的传入一个请求体即可

public static String delete(String src,String json){// 实例化OkHttpClient OkHttpClient okHttpClient = new OkHttpClient();// 构建请求体 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),json);// 构建request请求 Request request = new Request.Builder() .url(src) .delete(requestBody) .build();// 发送请求获取返回数据 Call call = okHttpClient.newCall(request);// 获取响应体 ResponseBody body = null; String string = null; try { body = call.execute().body();// 转换成string string = body.string(); } catch (IOException e) { e.printStackTrace(); } return string; }

不携带参数则时在get请求上重写,将.get修改为.delete即可

public static String delete(String src){// 实例化OkHttpClient OkHttpClient okHttpClient = new OkHttpClient();// 构建request请求 Request request = new Request.Builder() .url(src) .delete() .build();// 发送请求获取返回数据 Call call = okHttpClient.newCall(request);// 获取响应体 ResponseBody body = null; String string = null; try { body = call.execute().body();// 转换成string string = body.string(); } catch (IOException e) { e.printStackTrace(); } return string; }

相信经历过HttpURLConnection那复杂的网络请求设置后,OkHttp框架将会让你更加快乐的进行网络开发。

修改默认设置

上面我们说过,OkHttp默认帮我们配置好了设置,那么我们不想使用默认设置该怎么办呢。

我们可以在创建OkHttpClient类的时候使用链式调用的方式来设置默认的参数,如

// 实例化OkHttpClient OkHttpClient okHttpClient = new OkHttpClient.Builder()// 一天 .connectTimeout(Duration.ofDays(1))// 系统默认dns .dns(Dns.SYSTEM) .build();

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK