13 changed files with 1726 additions and 15 deletions
-
10app/build.gradle
-
5app/src/main/AndroidManifest.xml
-
317app/src/main/java/com/ouxuan/oxface/MainActivity.java
-
20app/src/main/java/com/ouxuan/oxface/OxFaceApplication.java
-
45app/src/main/java/com/ouxuan/oxface/network/NetworkConfig.java
-
222app/src/main/java/com/ouxuan/oxface/network/NetworkManager.java
-
304app/src/main/java/com/ouxuan/oxface/network/api/PadApiService.java
-
215app/src/main/java/com/ouxuan/oxface/network/api/UserApiService.java
-
44app/src/main/java/com/ouxuan/oxface/network/callback/NetworkCallback.java
-
62app/src/main/java/com/ouxuan/oxface/network/interceptor/HeaderInterceptor.java
-
67app/src/main/java/com/ouxuan/oxface/network/model/ApiResponse.java
-
422app/src/main/java/com/ouxuan/oxface/network/utils/NetworkUtils.java
-
4app/src/main/res/layout/dialog_login_success.xml
@ -0,0 +1,20 @@ |
|||
package com.ouxuan.oxface; |
|||
|
|||
import android.app.Application; |
|||
|
|||
import com.ouxuan.oxface.network.utils.NetworkUtils; |
|||
|
|||
/** |
|||
* 应用程序Application类 |
|||
* 用于全局初始化各种模块 |
|||
*/ |
|||
public class OxFaceApplication extends Application { |
|||
|
|||
@Override |
|||
public void onCreate() { |
|||
super.onCreate(); |
|||
|
|||
// 初始化网络模块 |
|||
NetworkUtils.init(this); |
|||
} |
|||
} |
@ -0,0 +1,45 @@ |
|||
package com.ouxuan.oxface.network; |
|||
|
|||
/** |
|||
* 网络配置常量类 |
|||
* 用于管理API地址、超时时间等网络相关配置 |
|||
*/ |
|||
public class NetworkConfig { |
|||
|
|||
// API基础URL - 欧轩智能测试环境 |
|||
public static final String BASE_URL = "https://testmanager.ouxuanzhineng.cn/"; |
|||
|
|||
// API版本路径 |
|||
public static final String API_VERSION = "v3/"; |
|||
|
|||
// 完整的API基础路径 |
|||
public static final String API_BASE_PATH = API_VERSION + "pad/"; |
|||
|
|||
// 连接超时时间(秒) |
|||
public static final int CONNECT_TIMEOUT = 30; |
|||
|
|||
// 读取超时时间(秒) |
|||
public static final int READ_TIMEOUT = 30; |
|||
|
|||
// 写入超时时间(秒) |
|||
public static final int WRITE_TIMEOUT = 30; |
|||
|
|||
// 是否开启日志打印(Debug模式) |
|||
public static final boolean ENABLE_LOG = true; |
|||
|
|||
// 通用请求头 |
|||
public static final String HEADER_CONTENT_TYPE = "Content-Type"; |
|||
public static final String HEADER_AUTHORIZATION = "Authorization"; |
|||
public static final String HEADER_USER_AGENT = "User-Agent"; |
|||
|
|||
// Content-Type类型 |
|||
public static final String CONTENT_TYPE_JSON = "application/json"; |
|||
public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"; |
|||
|
|||
// HTTP状态码 |
|||
public static final int HTTP_SUCCESS = 200; |
|||
public static final int HTTP_UNAUTHORIZED = 401; |
|||
public static final int HTTP_FORBIDDEN = 403; |
|||
public static final int HTTP_NOT_FOUND = 404; |
|||
public static final int HTTP_SERVER_ERROR = 500; |
|||
} |
@ -0,0 +1,222 @@ |
|||
package com.ouxuan.oxface.network; |
|||
|
|||
import android.content.Context; |
|||
import android.os.Handler; |
|||
import android.os.Looper; |
|||
|
|||
import com.google.gson.Gson; |
|||
import com.google.gson.GsonBuilder; |
|||
import com.ouxuan.oxface.network.callback.NetworkCallback; |
|||
import com.ouxuan.oxface.network.interceptor.HeaderInterceptor; |
|||
import com.ouxuan.oxface.network.model.ApiResponse; |
|||
|
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
import okhttp3.Call; |
|||
import okhttp3.Callback; |
|||
import okhttp3.MediaType; |
|||
import okhttp3.OkHttpClient; |
|||
import okhttp3.Request; |
|||
import okhttp3.RequestBody; |
|||
import okhttp3.Response; |
|||
import okhttp3.logging.HttpLoggingInterceptor; |
|||
import retrofit2.Retrofit; |
|||
import retrofit2.converter.gson.GsonConverterFactory; |
|||
|
|||
/** |
|||
* 网络请求管理器 |
|||
* 单例模式,统一管理所有网络请求 |
|||
*/ |
|||
public class NetworkManager { |
|||
|
|||
private static volatile NetworkManager instance; |
|||
private OkHttpClient okHttpClient; |
|||
private Retrofit retrofit; |
|||
private Gson gson; |
|||
private Handler mainHandler; |
|||
|
|||
private NetworkManager(Context context) { |
|||
initOkHttpClient(context); |
|||
initRetrofit(); |
|||
initGson(); |
|||
mainHandler = new Handler(Looper.getMainLooper()); |
|||
} |
|||
|
|||
/** |
|||
* 获取NetworkManager单例实例 |
|||
* @param context 上下文 |
|||
* @return NetworkManager实例 |
|||
*/ |
|||
public static NetworkManager getInstance(Context context) { |
|||
if (instance == null) { |
|||
synchronized (NetworkManager.class) { |
|||
if (instance == null) { |
|||
instance = new NetworkManager(context.getApplicationContext()); |
|||
} |
|||
} |
|||
} |
|||
return instance; |
|||
} |
|||
|
|||
/** |
|||
* 初始化OkHttpClient |
|||
*/ |
|||
private void initOkHttpClient(Context context) { |
|||
OkHttpClient.Builder builder = new OkHttpClient.Builder() |
|||
.connectTimeout(NetworkConfig.CONNECT_TIMEOUT, TimeUnit.SECONDS) |
|||
.readTimeout(NetworkConfig.READ_TIMEOUT, TimeUnit.SECONDS) |
|||
.writeTimeout(NetworkConfig.WRITE_TIMEOUT, TimeUnit.SECONDS) |
|||
.addInterceptor(new HeaderInterceptor(context)); |
|||
|
|||
// 添加日志拦截器(仅在Debug模式下) |
|||
if (NetworkConfig.ENABLE_LOG) { |
|||
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); |
|||
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); |
|||
builder.addInterceptor(loggingInterceptor); |
|||
} |
|||
|
|||
okHttpClient = builder.build(); |
|||
} |
|||
|
|||
/** |
|||
* 初始化Retrofit |
|||
*/ |
|||
private void initRetrofit() { |
|||
retrofit = new Retrofit.Builder() |
|||
.baseUrl(NetworkConfig.BASE_URL) |
|||
.client(okHttpClient) |
|||
.addConverterFactory(GsonConverterFactory.create()) |
|||
.build(); |
|||
} |
|||
|
|||
/** |
|||
* 初始化Gson |
|||
*/ |
|||
private void initGson() { |
|||
gson = new GsonBuilder() |
|||
.setDateFormat("yyyy-MM-dd HH:mm:ss") |
|||
.create(); |
|||
} |
|||
|
|||
/** |
|||
* 获取Retrofit实例 |
|||
* @return Retrofit实例 |
|||
*/ |
|||
public Retrofit getRetrofit() { |
|||
return retrofit; |
|||
} |
|||
|
|||
/** |
|||
* 创建API服务接口 |
|||
* @param serviceClass 服务接口类 |
|||
* @param <T> 服务接口类型 |
|||
* @return 服务接口实例 |
|||
*/ |
|||
public <T> T createService(Class<T> serviceClass) { |
|||
return retrofit.create(serviceClass); |
|||
} |
|||
|
|||
/** |
|||
* 执行GET请求 |
|||
* @param url 请求URL |
|||
* @param callback 回调接口 |
|||
* @param <T> 响应数据类型 |
|||
*/ |
|||
public <T> void get(String url, NetworkCallback<T> callback) { |
|||
Request request = new Request.Builder() |
|||
.url(url) |
|||
.get() |
|||
.build(); |
|||
|
|||
executeRequest(request, callback); |
|||
} |
|||
|
|||
/** |
|||
* 执行POST请求 |
|||
* @param url 请求URL |
|||
* @param jsonBody JSON格式的请求体 |
|||
* @param callback 回调接口 |
|||
* @param <T> 响应数据类型 |
|||
*/ |
|||
public <T> void post(String url, String jsonBody, NetworkCallback<T> callback) { |
|||
RequestBody requestBody = RequestBody.create( |
|||
MediaType.parse(NetworkConfig.CONTENT_TYPE_JSON), |
|||
jsonBody |
|||
); |
|||
|
|||
Request request = new Request.Builder() |
|||
.url(url) |
|||
.post(requestBody) |
|||
.build(); |
|||
|
|||
executeRequest(request, callback); |
|||
} |
|||
|
|||
/** |
|||
* 执行POST请求(传入对象,自动转换为JSON) |
|||
* @param url 请求URL |
|||
* @param requestObject 请求对象 |
|||
* @param callback 回调接口 |
|||
* @param <T> 响应数据类型 |
|||
*/ |
|||
public <T> void post(String url, Object requestObject, NetworkCallback<T> callback) { |
|||
String jsonBody = gson.toJson(requestObject); |
|||
post(url, jsonBody, callback); |
|||
} |
|||
|
|||
/** |
|||
* 执行网络请求 |
|||
* @param request 请求对象 |
|||
* @param callback 回调接口 |
|||
* @param <T> 响应数据类型 |
|||
*/ |
|||
private <T> void executeRequest(Request request, NetworkCallback<T> callback) { |
|||
// 主线程回调请求开始 |
|||
mainHandler.post(() -> callback.onStart()); |
|||
|
|||
okHttpClient.newCall(request).enqueue(new Callback() { |
|||
@Override |
|||
public void onResponse(Call call, Response response) { |
|||
try { |
|||
String responseBody = response.body().string(); |
|||
|
|||
if (response.isSuccessful()) { |
|||
// 解析响应数据 |
|||
ApiResponse<T> apiResponse = gson.fromJson(responseBody, ApiResponse.class); |
|||
|
|||
// 主线程回调结果 |
|||
mainHandler.post(() -> { |
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
callback.onComplete(); |
|||
}); |
|||
} else { |
|||
// HTTP错误 |
|||
mainHandler.post(() -> { |
|||
callback.onError(response.code(), "HTTP Error: " + response.message()); |
|||
callback.onComplete(); |
|||
}); |
|||
} |
|||
} catch (Exception e) { |
|||
// 解析异常 |
|||
mainHandler.post(() -> { |
|||
callback.onException(e); |
|||
callback.onComplete(); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call call, java.io.IOException e) { |
|||
// 网络请求失败 |
|||
mainHandler.post(() -> { |
|||
callback.onException(e); |
|||
callback.onComplete(); |
|||
}); |
|||
} |
|||
}); |
|||
} |
|||
} |
@ -0,0 +1,304 @@ |
|||
package com.ouxuan.oxface.network.api; |
|||
|
|||
import com.google.gson.annotations.SerializedName; |
|||
import com.ouxuan.oxface.network.model.ApiResponse; |
|||
|
|||
import retrofit2.Call; |
|||
import retrofit2.http.Body; |
|||
import retrofit2.http.GET; |
|||
import retrofit2.http.POST; |
|||
import retrofit2.http.Query; |
|||
|
|||
/** |
|||
* Pad相关API接口 |
|||
* 对应旧UniApp中的pad相关接口 |
|||
*/ |
|||
public interface PadApiService { |
|||
|
|||
/** |
|||
* Pad登录接口 |
|||
* 对应旧接口: /v3/pad/login |
|||
* @param loginRequest 登录请求体 |
|||
* @return 登录响应 |
|||
*/ |
|||
@POST("v3/pad/login") |
|||
Call<ApiResponse<PadLoginResponse>> padLogin(@Body PadLoginRequest loginRequest); |
|||
|
|||
/** |
|||
* 获取Pad列表 |
|||
* 对应旧接口: /v3/pad/list |
|||
* @return Pad列表响应 |
|||
*/ |
|||
@GET("v3/pad/list") |
|||
Call<ApiResponse<PadListResponse>> padList(); |
|||
|
|||
/** |
|||
* 获取Pad列表(带查询参数) |
|||
* @param userId 用户ID(可选) |
|||
* @param status 状态筛选(可选) |
|||
* @return Pad列表响应 |
|||
*/ |
|||
@GET("v3/pad/list") |
|||
Call<ApiResponse<PadListResponse>> padList(@Query("userId") String userId, |
|||
@Query("status") String status); |
|||
|
|||
/** |
|||
* Pad选择确认 |
|||
* 对应旧接口: /v3/pad/select |
|||
* @param selectRequest 选择请求体 |
|||
* @return 选择确认响应 |
|||
*/ |
|||
@POST("v3/pad/select") |
|||
Call<ApiResponse<PadSelectResponse>> padSelect(@Body PadSelectRequest selectRequest); |
|||
|
|||
// ==================== 请求和响应数据模型 ==================== |
|||
|
|||
/** |
|||
* Pad登录请求模型 |
|||
*/ |
|||
public static class PadLoginRequest { |
|||
private String username; // 用户名 |
|||
private String password; // 密码 |
|||
private String deviceId; // 设备ID |
|||
private String deviceType; // 设备类型(如:android) |
|||
private String appVersion; // 应用版本 |
|||
|
|||
public PadLoginRequest(String username, String password, String deviceId) { |
|||
this.username = username; |
|||
this.password = password; |
|||
this.deviceId = deviceId; |
|||
this.deviceType = "android"; |
|||
this.appVersion = "1.0.0"; |
|||
} |
|||
|
|||
// Getters and Setters |
|||
public String getUsername() { return username; } |
|||
public void setUsername(String username) { this.username = username; } |
|||
|
|||
public String getPassword() { return password; } |
|||
public void setPassword(String password) { this.password = password; } |
|||
|
|||
public String getDeviceId() { return deviceId; } |
|||
public void setDeviceId(String deviceId) { this.deviceId = deviceId; } |
|||
|
|||
public String getDeviceType() { return deviceType; } |
|||
public void setDeviceType(String deviceType) { this.deviceType = deviceType; } |
|||
|
|||
public String getAppVersion() { return appVersion; } |
|||
public void setAppVersion(String appVersion) { this.appVersion = appVersion; } |
|||
} |
|||
|
|||
/** |
|||
* Pad登录响应模型 - 匹配实际服务器返回结构 |
|||
*/ |
|||
public static class PadLoginResponse { |
|||
@SerializedName("brand_id") |
|||
private int brand_id; // 品牌ID |
|||
|
|||
@SerializedName("logo") |
|||
private String logo; // Logo URL |
|||
|
|||
@SerializedName("name") |
|||
private String name; // 门店名称 |
|||
|
|||
@SerializedName("stadium_id") |
|||
private int stadium_id; // 场馆ID |
|||
|
|||
@SerializedName("token") |
|||
private String token; // 访问令牌 |
|||
|
|||
// Getters and Setters |
|||
public int getBrandId() { return brand_id; } |
|||
public void setBrandId(int brand_id) { this.brand_id = brand_id; } |
|||
|
|||
public String getLogo() { return logo; } |
|||
public void setLogo(String logo) { this.logo = logo; } |
|||
|
|||
public String getName() { return name; } |
|||
public void setName(String name) { this.name = name; } |
|||
|
|||
public int getStadiumId() { return stadium_id; } |
|||
public void setStadiumId(int stadium_id) { this.stadium_id = stadium_id; } |
|||
|
|||
public String getToken() { return token; } |
|||
public void setToken(String token) { this.token = token; } |
|||
|
|||
// 为了兼容原有代码,保留一些方法 |
|||
public UserInfo getUserInfo() { |
|||
// 根据实际数据构造UserInfo对象 |
|||
UserInfo userInfo = new UserInfo(); |
|||
userInfo.setStoreName(this.name); |
|||
userInfo.setStoreId(String.valueOf(this.stadium_id)); |
|||
return userInfo; |
|||
} |
|||
|
|||
public String getSessionId() { |
|||
// 从token中提取会话信息,或返回token作为sessionId |
|||
return this.token; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "PadLoginResponse{" + |
|||
"brand_id=" + brand_id + |
|||
", logo='" + logo + '\'' + |
|||
", name='" + name + '\'' + |
|||
", stadium_id=" + stadium_id + |
|||
", token='" + token + '\'' + |
|||
'}'; |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* 用户信息模型 |
|||
*/ |
|||
public static class UserInfo { |
|||
private String userId; // 用户ID |
|||
private String username; // 用户名 |
|||
private String nickname; // 昵称 |
|||
private String role; // 角色 |
|||
private String storeId; // 店铺ID |
|||
private String storeName; // 店铺名称 |
|||
private String permissions; // 权限列表 |
|||
|
|||
// Getters and Setters |
|||
public String getUserId() { return userId; } |
|||
public void setUserId(String userId) { this.userId = userId; } |
|||
|
|||
public String getUsername() { return username; } |
|||
public void setUsername(String username) { this.username = username; } |
|||
|
|||
public String getNickname() { return nickname; } |
|||
public void setNickname(String nickname) { this.nickname = nickname; } |
|||
|
|||
public String getRole() { return role; } |
|||
public void setRole(String role) { this.role = role; } |
|||
|
|||
public String getStoreId() { return storeId; } |
|||
public void setStoreId(String storeId) { this.storeId = storeId; } |
|||
|
|||
public String getStoreName() { return storeName; } |
|||
public void setStoreName(String storeName) { this.storeName = storeName; } |
|||
|
|||
public String getPermissions() { return permissions; } |
|||
public void setPermissions(String permissions) { this.permissions = permissions; } |
|||
} |
|||
|
|||
/** |
|||
* Pad列表响应模型 |
|||
*/ |
|||
public static class PadListResponse { |
|||
private java.util.List<PadInfo> padList; // Pad设备列表 |
|||
private int totalCount; // 总数量 |
|||
private String lastUpdateTime; // 最后更新时间 |
|||
|
|||
// Getters and Setters |
|||
public java.util.List<PadInfo> getPadList() { return padList; } |
|||
public void setPadList(java.util.List<PadInfo> padList) { this.padList = padList; } |
|||
|
|||
public int getTotalCount() { return totalCount; } |
|||
public void setTotalCount(int totalCount) { this.totalCount = totalCount; } |
|||
|
|||
public String getLastUpdateTime() { return lastUpdateTime; } |
|||
public void setLastUpdateTime(String lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; } |
|||
} |
|||
|
|||
/** |
|||
* Pad设备信息模型 |
|||
*/ |
|||
public static class PadInfo { |
|||
private String padId; // Pad ID |
|||
private String padName; // Pad名称 |
|||
private String padType; // Pad类型 |
|||
private String status; // 状态(online/offline/busy) |
|||
private String location; // 位置 |
|||
private String description; // 描述 |
|||
private boolean isSelected; // 是否已选择 |
|||
private String lastActiveTime; // 最后活跃时间 |
|||
|
|||
// Getters and Setters |
|||
public String getPadId() { return padId; } |
|||
public void setPadId(String padId) { this.padId = padId; } |
|||
|
|||
public String getPadName() { return padName; } |
|||
public void setPadName(String padName) { this.padName = padName; } |
|||
|
|||
public String getPadType() { return padType; } |
|||
public void setPadType(String padType) { this.padType = padType; } |
|||
|
|||
public String getStatus() { return status; } |
|||
public void setStatus(String status) { this.status = status; } |
|||
|
|||
public String getLocation() { return location; } |
|||
public void setLocation(String location) { this.location = location; } |
|||
|
|||
public String getDescription() { return description; } |
|||
public void setDescription(String description) { this.description = description; } |
|||
|
|||
public boolean isSelected() { return isSelected; } |
|||
public void setSelected(boolean selected) { isSelected = selected; } |
|||
|
|||
public String getLastActiveTime() { return lastActiveTime; } |
|||
public void setLastActiveTime(String lastActiveTime) { this.lastActiveTime = lastActiveTime; } |
|||
} |
|||
|
|||
/** |
|||
* Pad选择请求模型 |
|||
*/ |
|||
public static class PadSelectRequest { |
|||
private String padId; // 选择的Pad ID |
|||
private String userId; // 用户ID |
|||
private String sessionId; // 会话ID |
|||
private String selectReason; // 选择原因(可选) |
|||
|
|||
public PadSelectRequest(String padId, String userId, String sessionId) { |
|||
this.padId = padId; |
|||
this.userId = userId; |
|||
this.sessionId = sessionId; |
|||
} |
|||
|
|||
// Getters and Setters |
|||
public String getPadId() { return padId; } |
|||
public void setPadId(String padId) { this.padId = padId; } |
|||
|
|||
public String getUserId() { return userId; } |
|||
public void setUserId(String userId) { this.userId = userId; } |
|||
|
|||
public String getSessionId() { return sessionId; } |
|||
public void setSessionId(String sessionId) { this.sessionId = sessionId; } |
|||
|
|||
public String getSelectReason() { return selectReason; } |
|||
public void setSelectReason(String selectReason) { this.selectReason = selectReason; } |
|||
} |
|||
|
|||
/** |
|||
* Pad选择响应模型 |
|||
*/ |
|||
public static class PadSelectResponse { |
|||
private String result; // 选择结果(success/failed) |
|||
private String message; // 响应消息 |
|||
private String selectedPadId; // 已选择的Pad ID |
|||
private String selectedPadName; // 已选择的Pad名称 |
|||
private String sessionToken; // 会话令牌 |
|||
private long validUntil; // 有效期至 |
|||
|
|||
// Getters and Setters |
|||
public String getResult() { return result; } |
|||
public void setResult(String result) { this.result = result; } |
|||
|
|||
public String getMessage() { return message; } |
|||
public void setMessage(String message) { this.message = message; } |
|||
|
|||
public String getSelectedPadId() { return selectedPadId; } |
|||
public void setSelectedPadId(String selectedPadId) { this.selectedPadId = selectedPadId; } |
|||
|
|||
public String getSelectedPadName() { return selectedPadName; } |
|||
public void setSelectedPadName(String selectedPadName) { this.selectedPadName = selectedPadName; } |
|||
|
|||
public String getSessionToken() { return sessionToken; } |
|||
public void setSessionToken(String sessionToken) { this.sessionToken = sessionToken; } |
|||
|
|||
public long getValidUntil() { return validUntil; } |
|||
public void setValidUntil(long validUntil) { this.validUntil = validUntil; } |
|||
} |
|||
} |
@ -0,0 +1,215 @@ |
|||
package com.ouxuan.oxface.network.api; |
|||
|
|||
import com.ouxuan.oxface.network.model.ApiResponse; |
|||
|
|||
import retrofit2.Call; |
|||
import retrofit2.http.Body; |
|||
import retrofit2.http.GET; |
|||
import retrofit2.http.POST; |
|||
import retrofit2.http.Query; |
|||
|
|||
/** |
|||
* 用户相关API接口 |
|||
* 使用Retrofit注解定义RESTful API |
|||
*/ |
|||
public interface UserApiService { |
|||
|
|||
/** |
|||
* 用户登录 |
|||
* @param loginRequest 登录请求体 |
|||
* @return 登录响应 |
|||
*/ |
|||
@POST("auth/login") |
|||
Call<ApiResponse<LoginResponse>> login(@Body LoginRequest loginRequest); |
|||
|
|||
/** |
|||
* 用户注册 |
|||
* @param registerRequest 注册请求体 |
|||
* @return 注册响应 |
|||
*/ |
|||
@POST("auth/register") |
|||
Call<ApiResponse<RegisterResponse>> register(@Body RegisterRequest registerRequest); |
|||
|
|||
/** |
|||
* 获取用户信息 |
|||
* @param userId 用户ID |
|||
* @return 用户信息响应 |
|||
*/ |
|||
@GET("user/info") |
|||
Call<ApiResponse<UserInfo>> getUserInfo(@Query("userId") String userId); |
|||
|
|||
/** |
|||
* 刷新Token |
|||
* @param refreshRequest 刷新Token请求 |
|||
* @return Token响应 |
|||
*/ |
|||
@POST("auth/refresh") |
|||
Call<ApiResponse<TokenResponse>> refreshToken(@Body RefreshTokenRequest refreshRequest); |
|||
|
|||
/** |
|||
* 用户登出 |
|||
* @return 登出响应 |
|||
*/ |
|||
@POST("auth/logout") |
|||
Call<ApiResponse<Void>> logout(); |
|||
|
|||
// ==================== 请求和响应数据模型 ==================== |
|||
|
|||
/** |
|||
* 登录请求模型 |
|||
*/ |
|||
public static class LoginRequest { |
|||
private String username; |
|||
private String password; |
|||
private String deviceId; |
|||
|
|||
public LoginRequest(String username, String password, String deviceId) { |
|||
this.username = username; |
|||
this.password = password; |
|||
this.deviceId = deviceId; |
|||
} |
|||
|
|||
// Getters and Setters |
|||
public String getUsername() { return username; } |
|||
public void setUsername(String username) { this.username = username; } |
|||
|
|||
public String getPassword() { return password; } |
|||
public void setPassword(String password) { this.password = password; } |
|||
|
|||
public String getDeviceId() { return deviceId; } |
|||
public void setDeviceId(String deviceId) { this.deviceId = deviceId; } |
|||
} |
|||
|
|||
/** |
|||
* 登录响应模型 |
|||
*/ |
|||
public static class LoginResponse { |
|||
private String token; |
|||
private String refreshToken; |
|||
private UserInfo userInfo; |
|||
private long expiresIn; |
|||
|
|||
// Getters and Setters |
|||
public String getToken() { return token; } |
|||
public void setToken(String token) { this.token = token; } |
|||
|
|||
public String getRefreshToken() { return refreshToken; } |
|||
public void setRefreshToken(String refreshToken) { this.refreshToken = refreshToken; } |
|||
|
|||
public UserInfo getUserInfo() { return userInfo; } |
|||
public void setUserInfo(UserInfo userInfo) { this.userInfo = userInfo; } |
|||
|
|||
public long getExpiresIn() { return expiresIn; } |
|||
public void setExpiresIn(long expiresIn) { this.expiresIn = expiresIn; } |
|||
} |
|||
|
|||
/** |
|||
* 用户信息模型 |
|||
*/ |
|||
public static class UserInfo { |
|||
private String userId; |
|||
private String username; |
|||
private String nickname; |
|||
private String email; |
|||
private String avatar; |
|||
private String role; |
|||
|
|||
// Getters and Setters |
|||
public String getUserId() { return userId; } |
|||
public void setUserId(String userId) { this.userId = userId; } |
|||
|
|||
public String getUsername() { return username; } |
|||
public void setUsername(String username) { this.username = username; } |
|||
|
|||
public String getNickname() { return nickname; } |
|||
public void setNickname(String nickname) { this.nickname = nickname; } |
|||
|
|||
public String getEmail() { return email; } |
|||
public void setEmail(String email) { this.email = email; } |
|||
|
|||
public String getAvatar() { return avatar; } |
|||
public void setAvatar(String avatar) { this.avatar = avatar; } |
|||
|
|||
public String getRole() { return role; } |
|||
public void setRole(String role) { this.role = role; } |
|||
} |
|||
|
|||
/** |
|||
* 注册请求模型 |
|||
*/ |
|||
public static class RegisterRequest { |
|||
private String username; |
|||
private String password; |
|||
private String email; |
|||
private String nickname; |
|||
|
|||
public RegisterRequest(String username, String password, String email, String nickname) { |
|||
this.username = username; |
|||
this.password = password; |
|||
this.email = email; |
|||
this.nickname = nickname; |
|||
} |
|||
|
|||
// Getters and Setters |
|||
public String getUsername() { return username; } |
|||
public void setUsername(String username) { this.username = username; } |
|||
|
|||
public String getPassword() { return password; } |
|||
public void setPassword(String password) { this.password = password; } |
|||
|
|||
public String getEmail() { return email; } |
|||
public void setEmail(String email) { this.email = email; } |
|||
|
|||
public String getNickname() { return nickname; } |
|||
public void setNickname(String nickname) { this.nickname = nickname; } |
|||
} |
|||
|
|||
/** |
|||
* 注册响应模型 |
|||
*/ |
|||
public static class RegisterResponse { |
|||
private String userId; |
|||
private String message; |
|||
|
|||
// Getters and Setters |
|||
public String getUserId() { return userId; } |
|||
public void setUserId(String userId) { this.userId = userId; } |
|||
|
|||
public String getMessage() { return message; } |
|||
public void setMessage(String message) { this.message = message; } |
|||
} |
|||
|
|||
/** |
|||
* Token响应模型 |
|||
*/ |
|||
public static class TokenResponse { |
|||
private String token; |
|||
private String refreshToken; |
|||
private long expiresIn; |
|||
|
|||
// Getters and Setters |
|||
public String getToken() { return token; } |
|||
public void setToken(String token) { this.token = token; } |
|||
|
|||
public String getRefreshToken() { return refreshToken; } |
|||
public void setRefreshToken(String refreshToken) { this.refreshToken = refreshToken; } |
|||
|
|||
public long getExpiresIn() { return expiresIn; } |
|||
public void setExpiresIn(long expiresIn) { this.expiresIn = expiresIn; } |
|||
} |
|||
|
|||
/** |
|||
* 刷新Token请求模型 |
|||
*/ |
|||
public static class RefreshTokenRequest { |
|||
private String refreshToken; |
|||
|
|||
public RefreshTokenRequest(String refreshToken) { |
|||
this.refreshToken = refreshToken; |
|||
} |
|||
|
|||
// Getters and Setters |
|||
public String getRefreshToken() { return refreshToken; } |
|||
public void setRefreshToken(String refreshToken) { this.refreshToken = refreshToken; } |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
package com.ouxuan.oxface.network.callback; |
|||
|
|||
/** |
|||
* 网络请求回调接口 |
|||
* @param <T> 响应数据类型 |
|||
*/ |
|||
public interface NetworkCallback<T> { |
|||
|
|||
/** |
|||
* 请求成功回调 |
|||
* @param data 响应数据 |
|||
*/ |
|||
void onSuccess(T data); |
|||
|
|||
/** |
|||
* 请求失败回调 |
|||
* @param errorCode 错误码 |
|||
* @param errorMessage 错误信息 |
|||
*/ |
|||
void onError(int errorCode, String errorMessage); |
|||
|
|||
/** |
|||
* 网络异常回调 |
|||
* @param throwable 异常信息 |
|||
*/ |
|||
default void onException(Throwable throwable) { |
|||
onError(-1, throwable.getMessage()); |
|||
} |
|||
|
|||
/** |
|||
* 请求开始回调(可选实现) |
|||
*/ |
|||
default void onStart() { |
|||
// 默认空实现 |
|||
} |
|||
|
|||
/** |
|||
* 请求完成回调(可选实现) |
|||
* 无论成功或失败都会调用 |
|||
*/ |
|||
default void onComplete() { |
|||
// 默认空实现 |
|||
} |
|||
} |
@ -0,0 +1,62 @@ |
|||
package com.ouxuan.oxface.network.interceptor; |
|||
|
|||
import android.content.Context; |
|||
import android.content.SharedPreferences; |
|||
|
|||
import com.ouxuan.oxface.network.NetworkConfig; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
import okhttp3.Interceptor; |
|||
import okhttp3.Request; |
|||
import okhttp3.Response; |
|||
|
|||
/** |
|||
* 请求头拦截器 |
|||
* 用于添加通用请求头,如Authorization、User-Agent等 |
|||
*/ |
|||
public class HeaderInterceptor implements Interceptor { |
|||
|
|||
private Context context; |
|||
|
|||
public HeaderInterceptor(Context context) { |
|||
this.context = context; |
|||
} |
|||
|
|||
@Override |
|||
public Response intercept(Chain chain) throws IOException { |
|||
Request originalRequest = chain.request(); |
|||
|
|||
// 构建新的请求,添加通用请求头 |
|||
Request.Builder requestBuilder = originalRequest.newBuilder() |
|||
.header(NetworkConfig.HEADER_CONTENT_TYPE, NetworkConfig.CONTENT_TYPE_JSON) |
|||
.header(NetworkConfig.HEADER_USER_AGENT, getUserAgent()) |
|||
.method(originalRequest.method(), originalRequest.body()); |
|||
|
|||
// 添加Authorization token(如果存在) |
|||
String token = getAuthToken(); |
|||
if (token != null && !token.isEmpty()) { |
|||
requestBuilder.header(NetworkConfig.HEADER_AUTHORIZATION, "Bearer " + token); |
|||
} |
|||
|
|||
Request newRequest = requestBuilder.build(); |
|||
return chain.proceed(newRequest); |
|||
} |
|||
|
|||
/** |
|||
* 获取用户认证Token |
|||
* @return token字符串 |
|||
*/ |
|||
private String getAuthToken() { |
|||
SharedPreferences prefs = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE); |
|||
return prefs.getString("auth_token", ""); |
|||
} |
|||
|
|||
/** |
|||
* 获取User-Agent |
|||
* @return User-Agent字符串 |
|||
*/ |
|||
private String getUserAgent() { |
|||
return "OxFaceAndroid/1.0 (Android)"; |
|||
} |
|||
} |
@ -0,0 +1,67 @@ |
|||
package com.ouxuan.oxface.network.model; |
|||
|
|||
/** |
|||
* 通用API响应数据模型 |
|||
* @param <T> 具体的数据类型 |
|||
*/ |
|||
public class ApiResponse<T> { |
|||
|
|||
private int code; // 响应状态码 |
|||
private String message; // 响应消息 |
|||
private T data; // 具体数据 |
|||
private boolean success; // 是否成功 |
|||
|
|||
public ApiResponse() { |
|||
} |
|||
|
|||
public ApiResponse(int code, String message, T data) { |
|||
this.code = code; |
|||
this.message = message; |
|||
this.data = data; |
|||
this.success = (code == 0); // 根据实际服务器响应,成功状态码是0 |
|||
} |
|||
|
|||
public int getCode() { |
|||
return code; |
|||
} |
|||
|
|||
public void setCode(int code) { |
|||
this.code = code; |
|||
this.success = (code == 0); // 根据实际服务器响应,成功状态码是0 |
|||
} |
|||
|
|||
public String getMessage() { |
|||
return message; |
|||
} |
|||
|
|||
public void setMessage(String message) { |
|||
this.message = message; |
|||
} |
|||
|
|||
public T getData() { |
|||
return data; |
|||
} |
|||
|
|||
public void setData(T data) { |
|||
this.data = data; |
|||
} |
|||
|
|||
public boolean isSuccess() { |
|||
// 基于code字段动态判断成功状态,而不依赖success字段 |
|||
return code == 0; |
|||
} |
|||
|
|||
public void setSuccess(boolean success) { |
|||
this.success = success; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "ApiResponse{" + |
|||
"code=" + code + |
|||
", message='" + message + '\'' + |
|||
", data=" + data + |
|||
", success=" + success + |
|||
'}'; |
|||
} |
|||
} |
@ -0,0 +1,422 @@ |
|||
package com.ouxuan.oxface.network.utils; |
|||
|
|||
import android.content.Context; |
|||
|
|||
import com.ouxuan.oxface.network.NetworkManager; |
|||
import com.ouxuan.oxface.network.api.PadApiService; |
|||
import com.ouxuan.oxface.network.api.UserApiService; |
|||
import com.ouxuan.oxface.network.callback.NetworkCallback; |
|||
import com.ouxuan.oxface.network.model.ApiResponse; |
|||
|
|||
import retrofit2.Call; |
|||
import retrofit2.Callback; |
|||
import retrofit2.Response; |
|||
|
|||
import retrofit2.Call; |
|||
import retrofit2.Callback; |
|||
import retrofit2.Response; |
|||
|
|||
/** |
|||
* 网络请求工具类 |
|||
* 提供便捷的网络请求方法 |
|||
*/ |
|||
public class NetworkUtils { |
|||
|
|||
private static NetworkManager networkManager; |
|||
private static UserApiService userApiService; |
|||
private static PadApiService padApiService; |
|||
|
|||
/** |
|||
* 初始化网络工具类 |
|||
* @param context 上下文 |
|||
*/ |
|||
public static void init(Context context) { |
|||
networkManager = NetworkManager.getInstance(context); |
|||
userApiService = networkManager.createService(UserApiService.class); |
|||
padApiService = networkManager.createService(PadApiService.class); |
|||
} |
|||
|
|||
/** |
|||
* 用户登录 |
|||
* @param username 用户名 |
|||
* @param password 密码 |
|||
* @param deviceId 设备ID |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void login(String username, String password, String deviceId, |
|||
NetworkCallback<UserApiService.LoginResponse> callback) { |
|||
if (userApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
UserApiService.LoginRequest request = new UserApiService.LoginRequest(username, password, deviceId); |
|||
|
|||
callback.onStart(); |
|||
userApiService.login(request).enqueue(new Callback<ApiResponse<UserApiService.LoginResponse>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<UserApiService.LoginResponse>> call, |
|||
Response<ApiResponse<UserApiService.LoginResponse>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<UserApiService.LoginResponse> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<UserApiService.LoginResponse>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 用户注册 |
|||
* @param username 用户名 |
|||
* @param password 密码 |
|||
* @param email 邮箱 |
|||
* @param nickname 昵称 |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void register(String username, String password, String email, String nickname, |
|||
NetworkCallback<UserApiService.RegisterResponse> callback) { |
|||
if (userApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
UserApiService.RegisterRequest request = new UserApiService.RegisterRequest(username, password, email, nickname); |
|||
|
|||
callback.onStart(); |
|||
userApiService.register(request).enqueue(new Callback<ApiResponse<UserApiService.RegisterResponse>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<UserApiService.RegisterResponse>> call, |
|||
Response<ApiResponse<UserApiService.RegisterResponse>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<UserApiService.RegisterResponse> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<UserApiService.RegisterResponse>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 获取用户信息 |
|||
* @param userId 用户ID |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void getUserInfo(String userId, NetworkCallback<UserApiService.UserInfo> callback) { |
|||
if (userApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
callback.onStart(); |
|||
userApiService.getUserInfo(userId).enqueue(new Callback<ApiResponse<UserApiService.UserInfo>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<UserApiService.UserInfo>> call, |
|||
Response<ApiResponse<UserApiService.UserInfo>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<UserApiService.UserInfo> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<UserApiService.UserInfo>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 用户登出 |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void logout(NetworkCallback<Void> callback) { |
|||
if (userApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
callback.onStart(); |
|||
userApiService.logout().enqueue(new Callback<ApiResponse<Void>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<Void>> call, |
|||
Response<ApiResponse<Void>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<Void> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(null); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<Void>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 获取NetworkManager实例 |
|||
* @return NetworkManager实例 |
|||
*/ |
|||
public static NetworkManager getNetworkManager() { |
|||
return networkManager; |
|||
} |
|||
|
|||
// ==================== Pad相关API调用方法 ==================== |
|||
|
|||
/** |
|||
* Pad登录 |
|||
* @param username 用户名 |
|||
* @param password 密码 |
|||
* @param deviceId 设备ID |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void padLogin(String username, String password, String deviceId, |
|||
NetworkCallback<PadApiService.PadLoginResponse> callback) { |
|||
if (padApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
PadApiService.PadLoginRequest request = new PadApiService.PadLoginRequest(username, password, deviceId); |
|||
|
|||
callback.onStart(); |
|||
padApiService.padLogin(request).enqueue(new Callback<ApiResponse<PadApiService.PadLoginResponse>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<PadApiService.PadLoginResponse>> call, |
|||
Response<ApiResponse<PadApiService.PadLoginResponse>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<PadApiService.PadLoginResponse> apiResponse = response.body(); |
|||
|
|||
// 添加详细的调试日志 |
|||
android.util.Log.d("NetworkUtils", "Response received - Code: " + apiResponse.getCode() + |
|||
", Message: '" + apiResponse.getMessage() + "'" + |
|||
", Success: " + apiResponse.isSuccess() + |
|||
", Data: " + (apiResponse.getData() != null ? "not null" : "null")); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
android.util.Log.d("NetworkUtils", "Calling onSuccess with data: " + apiResponse.getData()); |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
android.util.Log.d("NetworkUtils", "Calling onError - Code: " + apiResponse.getCode() + ", Message: " + apiResponse.getMessage()); |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
android.util.Log.e("NetworkUtils", "Response not successful or body is null - Code: " + response.code()); |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<PadApiService.PadLoginResponse>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 获取Pad列表 |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void getPadList(NetworkCallback<PadApiService.PadListResponse> callback) { |
|||
if (padApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
callback.onStart(); |
|||
padApiService.padList().enqueue(new Callback<ApiResponse<PadApiService.PadListResponse>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<PadApiService.PadListResponse>> call, |
|||
Response<ApiResponse<PadApiService.PadListResponse>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<PadApiService.PadListResponse> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<PadApiService.PadListResponse>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* 获取Pad列表(带查询参数) |
|||
* @param userId 用户ID |
|||
* @param status 状态筛选 |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void getPadList(String userId, String status, |
|||
NetworkCallback<PadApiService.PadListResponse> callback) { |
|||
if (padApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
callback.onStart(); |
|||
padApiService.padList(userId, status).enqueue(new Callback<ApiResponse<PadApiService.PadListResponse>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<PadApiService.PadListResponse>> call, |
|||
Response<ApiResponse<PadApiService.PadListResponse>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<PadApiService.PadListResponse> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<PadApiService.PadListResponse>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* Pad选择确认 |
|||
* @param padId 选择的Pad ID |
|||
* @param userId 用户ID |
|||
* @param sessionId 会话ID |
|||
* @param callback 回调接口 |
|||
*/ |
|||
public static void selectPad(String padId, String userId, String sessionId, |
|||
NetworkCallback<PadApiService.PadSelectResponse> callback) { |
|||
if (padApiService == null) { |
|||
callback.onError(-1, "NetworkUtils未初始化,请先调用init()方法"); |
|||
return; |
|||
} |
|||
|
|||
PadApiService.PadSelectRequest request = new PadApiService.PadSelectRequest(padId, userId, sessionId); |
|||
|
|||
callback.onStart(); |
|||
padApiService.padSelect(request).enqueue(new Callback<ApiResponse<PadApiService.PadSelectResponse>>() { |
|||
@Override |
|||
public void onResponse(Call<ApiResponse<PadApiService.PadSelectResponse>> call, |
|||
Response<ApiResponse<PadApiService.PadSelectResponse>> response) { |
|||
try { |
|||
if (response.isSuccessful() && response.body() != null) { |
|||
ApiResponse<PadApiService.PadSelectResponse> apiResponse = response.body(); |
|||
|
|||
if (apiResponse.isSuccess()) { |
|||
callback.onSuccess(apiResponse.getData()); |
|||
} else { |
|||
callback.onError(apiResponse.getCode(), apiResponse.getMessage()); |
|||
} |
|||
} else { |
|||
callback.onError(response.code(), "请求失败: " + response.message()); |
|||
} |
|||
} catch (Exception e) { |
|||
callback.onException(e); |
|||
} finally { |
|||
callback.onComplete(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(Call<ApiResponse<PadApiService.PadSelectResponse>> call, Throwable t) { |
|||
callback.onException(t); |
|||
callback.onComplete(); |
|||
} |
|||
}); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue