Json数据适配吻合
This commit is contained in:
		| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Thu Jun 05 04:02:31 GMT 2025 | ||||
| #Thu Jun 05 07:02:58 GMT 2025 | ||||
| stageCount=8 | ||||
| libraryProject=libappbase | ||||
| baseVersion=15.8 | ||||
| publishVersion=15.8.7 | ||||
| buildCount=1 | ||||
| buildCount=12 | ||||
| baseBetaVersion=15.8.8 | ||||
|   | ||||
| @@ -24,7 +24,7 @@ dependencies { | ||||
|     api fileTree(dir: 'libs', include: ['*.jar']) | ||||
|     // 网络连接类库 | ||||
|     api 'com.squareup.okhttp3:okhttp:4.4.1' | ||||
|     api 'com.fasterxml.jackson.core:jackson-databind:2.15.2' | ||||
|     api 'com.fasterxml.jackson.core:jackson-databind:2.18.2' | ||||
|     // 添加Gson依赖 | ||||
|     api 'com.google.code.gson:gson:2.10.1' | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Thu Jun 05 04:02:31 GMT 2025 | ||||
| #Thu Jun 05 07:02:58 GMT 2025 | ||||
| stageCount=8 | ||||
| libraryProject=libappbase | ||||
| baseVersion=15.8 | ||||
| publishVersion=15.8.7 | ||||
| buildCount=1 | ||||
| buildCount=12 | ||||
| baseBetaVersion=15.8.8 | ||||
|   | ||||
| @@ -28,6 +28,7 @@ public class LogonActivity extends Activity implements IWinBoLLActivity { | ||||
|     public static final String DEBUG_HOST = "http://10.8.0.250:456"; | ||||
|     public static final String YUN_HOST = "https://yun.winboll.cc"; | ||||
|      | ||||
|      | ||||
|     String mHost = ""; | ||||
|     RadioButton mrbYunHost; | ||||
|     RadioButton mrbDebugHost; | ||||
|   | ||||
| @@ -7,32 +7,51 @@ package cc.winboll.studio.libappbase.models; | ||||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||||
|  | ||||
| public class ResponseData { | ||||
|      | ||||
|     public static final String STATUS_SUCCESS = "success"; | ||||
|     public static final String STATUS_ERROR = "error"; | ||||
|      | ||||
|     @JsonProperty("status") | ||||
|     private String status; | ||||
|     @JsonProperty("message") | ||||
|     private String message; | ||||
|  | ||||
|     // ❌ 原代码只有带参构造函数,缺少无参构造函数 | ||||
|     // public ResponseData(String status, String message) { | ||||
|     //     this.status = status; | ||||
|     //     this.message = message; | ||||
|     // } | ||||
|  | ||||
|     // ✅ 添加无参构造函数(Jackson 反序列化必需) | ||||
|     @JsonProperty("data") | ||||
|     private UserInfoModel data; | ||||
|      | ||||
|     public ResponseData() { | ||||
|         // 空实现 | ||||
|         this.status = ""; | ||||
|         this.message = ""; | ||||
|         this.data = new UserInfoModel(); | ||||
|     } | ||||
|      | ||||
|     public ResponseData(String status, String message, UserInfoModel data) { | ||||
|         this.status = status; | ||||
|         this.message = message; | ||||
|         this.data = data; | ||||
|     } | ||||
|  | ||||
|     // 保留带参构造函数(可选,用于手动创建实例) | ||||
|     public ResponseData(String status, String message) { | ||||
|     public void setStatus(String status) { | ||||
|         this.status = status; | ||||
|     } | ||||
|  | ||||
|     public String getStatus() { | ||||
|         return status; | ||||
|     } | ||||
|  | ||||
|     public void setMessage(String message) { | ||||
|         this.message = message; | ||||
|     } | ||||
|  | ||||
|     // Getter 和 Setter | ||||
|     public String getStatus() { return status; } | ||||
|     public void setStatus(String status) { this.status = status; } | ||||
|     public String getMessage() { return message; } | ||||
|     public void setMessage(String message) { this.message = message; } | ||||
|     public String getMessage() { | ||||
|         return message; | ||||
|     } | ||||
|  | ||||
|     public void setData(UserInfoModel data) { | ||||
|         this.data = data; | ||||
|     } | ||||
|  | ||||
|     public UserInfoModel getData() { | ||||
|         return data; | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -9,10 +9,12 @@ import android.content.Context; | ||||
| import android.os.Handler; | ||||
| import android.os.Looper; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import cc.winboll.studio.libappbase.models.ResponseData; | ||||
| import cc.winboll.studio.libappbase.models.UserInfoModel; | ||||
| import com.google.gson.Gson; | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.net.URLDecoder; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.security.KeyPair; | ||||
| import java.security.PrivateKey; | ||||
| @@ -25,6 +27,7 @@ import okhttp3.OkHttpClient; | ||||
| import okhttp3.Request; | ||||
| import okhttp3.RequestBody; | ||||
| import okhttp3.Response; | ||||
| import java.io.UnsupportedEncodingException; | ||||
|  | ||||
| public class YunUtils { | ||||
|     public static final String TAG = "YunUtils"; | ||||
| @@ -95,24 +98,26 @@ public class YunUtils { | ||||
|                 @Override | ||||
|                 public void onSuccess(String responseBody) { | ||||
|                     LogUtils.d(TAG, "onSuccess"); | ||||
|                     try { | ||||
|                         String formattedJson = JsonFormatter.formatUnicodeToChinese(responseBody); | ||||
|                         LogUtils.d(TAG, formattedJson); | ||||
|                         return; | ||||
|                     } catch (Exception e) { | ||||
|                         LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
|                     } | ||||
|                      | ||||
|                     try { | ||||
|                         UserInfoModel userInfoModel = UserInfoModel.parseStringToBean(responseBody, UserInfoModel.class); | ||||
|                         if (userInfoModel != null) { | ||||
|                             LogUtils.d(TAG, "收到网站 UserInfoModel"); | ||||
|                             String token = userInfoModel.getToken(); | ||||
|                             saveLocalToken(token); | ||||
|                             checkLoginStatus(); | ||||
|                     LogUtils.d(TAG, String.format("responseBody %s", responseBody)); | ||||
|                     Gson gson = new Gson(); | ||||
|                     ResponseData result = gson.fromJson(responseBody, ResponseData.class); // 转为 Result 实例 | ||||
|                     if(result.getStatus().equals(ResponseData.STATUS_SUCCESS)) { | ||||
|                          | ||||
|                             UserInfoModel userInfoModel = result.getData(); | ||||
|                             if (userInfoModel != null) { | ||||
|                                 LogUtils.d(TAG, "收到网站 UserInfoModel"); | ||||
|                                 String token = userInfoModel.getToken(); | ||||
|                                 saveLocalToken(token); | ||||
|                                 checkLoginStatus(); | ||||
|                             } | ||||
|                         | ||||
|                     } else if(result.getStatus().equals(ResponseData.STATUS_ERROR)) { | ||||
|                         try { | ||||
|                             String decodedMessage = URLDecoder.decode(result.getMessage(), "UTF-8"); | ||||
|                             LogUtils.d(TAG, "服务器返回信息: " + decodedMessage); | ||||
|                         } catch (UnsupportedEncodingException e) { | ||||
|                             LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
|                         } | ||||
|                     } catch (IOException e) { | ||||
|                         LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen