[feature]网络配置
This commit is contained in:
parent
23a43eae08
commit
1b1e765e22
|
@ -56,7 +56,7 @@ dependencies {
|
|||
//运行时权限
|
||||
implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.3@aar'
|
||||
//网络通信框架
|
||||
implementation 'com.squareup.okhttp3:okhttp:3.8.0'
|
||||
// implementation 'com.squareup.okhttp3:okhttp:3.8.0'
|
||||
//netty
|
||||
// implementation 'com.littlegreens.netty.client:nettyclientlib:1.0.5'
|
||||
|
||||
|
@ -84,4 +84,13 @@ dependencies {
|
|||
|
||||
//android工具类
|
||||
implementation 'com.blankj:utilcodex:1.26.0'
|
||||
|
||||
//网络请求框架
|
||||
//添加okhttp依赖/
|
||||
implementation("com.squareup.okhttp3:okhttp:4.9.3")
|
||||
//添加Retrofit依赖
|
||||
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
|
||||
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
|
||||
//拦截日志依赖
|
||||
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package com.eningqu.aipen.network;
|
||||
|
||||
public interface Apis {
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.eningqu.aipen.network;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.eningqu.aipen.utils.GlobalConfig;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
/**
|
||||
* Package:com.eningqu.aipen.network
|
||||
* Author:starr
|
||||
* Time:2023/10/3 20:57
|
||||
* Description: 网络请求封装类
|
||||
*/
|
||||
public class RetrofitUtils {
|
||||
private static volatile Retrofit retrofit = null;
|
||||
|
||||
public Retrofit getInstance() {
|
||||
|
||||
if (retrofit == null) {
|
||||
synchronized (this) {
|
||||
if (retrofit == null) {
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder()
|
||||
.connectTimeout(60, TimeUnit.SECONDS)//连接超时时间60s
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.writeTimeout(60, TimeUnit.SECONDS);
|
||||
//根据需要可以设置其他的拦截器
|
||||
//...
|
||||
//日志拦截器
|
||||
HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
|
||||
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message ->
|
||||
Log.d("RetrofitMessage", "Okhttp====Message:" + message));
|
||||
loggingInterceptor.setLevel(level);
|
||||
//定制builder
|
||||
builder.addInterceptor(loggingInterceptor);
|
||||
retrofit = new Retrofit.Builder()
|
||||
.baseUrl(GlobalConfig.getBaseServerUrl())
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.client(builder.build())
|
||||
.build();
|
||||
return retrofit;
|
||||
}
|
||||
}
|
||||
}
|
||||
return retrofit;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.eningqu.aipen.utils;
|
||||
|
||||
/**
|
||||
* Package:com.eningqu.aipen.utils
|
||||
* Author:starr
|
||||
* Time:2023/10/3 21:19
|
||||
* Description: 全局配置
|
||||
*/
|
||||
public class GlobalConfig {
|
||||
|
||||
|
||||
public static String getBaseServerUrl() {
|
||||
return "http://smartedu.yongxinxue.com/";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue