内容简介:目前接口使用同异步的网络请求.enqueue();直接调用 Server.getModuleList()方法就可以在使用过程中可以根据需求调整
- RequestData:网络返回的数据
- Http:retrofit的网络实例化
- RetrofitService:网络的接口注解显示
- Server:网络接口的调用显示
- SSLSocketFactoryUtils:https的处理
1.配置build.gradle
implementation 'com.squareup.retrofit2:retrofit:2.4.0' //okhttp3的日志打印 implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' //retrofit的gson解析 implementation 'com.squareup.retrofit2:converter-gson:2.4.0' 复制代码
2.新建网络返回数据RequestData
public class RequestData { private String msg; private int status; private ArrayList<Data> data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public ArrayList<Data> getData() { return data; } public void setData(ArrayList<Data> data) { this.data = data; } @Override public String toString() { return "msg = " + msg + "status = " + status; } } 复制代码
3.添加https的校验
public class SSLSocketFactoryUtils { /* * 默认信任所有的证书 * */ public static SSLSocketFactory createSSLSocketFactory() { SSLSocketFactory sslSocketFactory = null; try { SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, new TrustManager[]{createTrustAllManager()}, new SecureRandom()); sslSocketFactory = sslContext.getSocketFactory(); } catch (Exception e) { } return sslSocketFactory; } public static X509TrustManager createTrustAllManager() { X509TrustManager tm = null; try { tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { //do nothing,接受任意客户端证书 } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { //do nothing,接受任意服务端证书 } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; } catch (Exception e) { } return tm; } } 复制代码
4.实例化Retrofit
public class Http { private static Http mInstance; //单利模式 public static Http getInstance() { if (mInstance == null) { synchronized (Http.class) { if (mInstance == null) { mInstance = new Http(); } } } return mInstance; } public Retrofit getRetrofit(){ HttpLoggingInterceptor loggingInterceptor; //打印网络请求相关日志 loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { //打印retrofit日志 Log.i("retrofitLog: ", message); } }); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); //为了打印日志,需要配置OkHttpClient OkHttpClient client = new OkHttpClient.Builder() //配置SSlSocketFactory .sslSocketFactory(SSLSocketFactoryUtils.createSSLSocketFactory()) .addInterceptor(loggingInterceptor) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Config.DOMAIN) // 设置 网络请求 Url .client(client) .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖) .build(); return retrofit; } public RetrofitService getRetrofitService(){ return getInstance().getRetrofit().create(RetrofitService.class); } } 复制代码
5.初始化RetrofitService接口注解
public interface RetrofitService { @GET("/api/user/modules") Call<RequestData> getModuleList(@Query("token") String token); @GET("/api/user/logout") Call<RequestData> logOutRequest(@Query("token") String token); } 复制代码
6.外部调用接口
public class Server { public static void getModuleList(String token,Callback<RequestData> callback){ Http.getInstance().getRetrofitService().getModuleList(token).enqueue(callback); } public static void logOutRequest(String token,Callback<RequestData> callback){ Http.getInstance().getRetrofitService().logOutRequest(token).enqueue(callback); } } 复制代码
目前接口使用同异步的网络请求.enqueue();
直接调用 Server.getModuleList()方法就可以
在使用过程中可以根据需求调整
以上所述就是小编给大家介绍的《Retrofit的简单封装》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 封装JDBC—非框架开发必备的封装类
- SpringBlade 2.3.2 发布,增加 OSS 封装及单元测试封装
- SpringBlade 2.3.2 发布,增加 OSS 封装及单元测试封装
- docker 封装 alinode
- 封装Apk签名工具
- axios封装笔记
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。