兼容后台接口不规范

This commit is contained in:
2024-10-23 15:41:12 +08:00
parent b64a200deb
commit 1dae6c840f
10 changed files with 99 additions and 107 deletions
@@ -12,54 +12,6 @@ public class CommonResponseBean<T> implements Serializable {
private int code;
private String msg;
// 数据未封装 无奈呀
private boolean captchaEnabled;
private String img;
private String uuid;
private int total;
private T rows;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public T getRows() {
return rows;
}
public void setRows(T rows) {
this.rows = rows;
}
public boolean isCaptchaEnabled() {
return captchaEnabled;
}
public void setCaptchaEnabled(boolean captchaEnabled) {
this.captchaEnabled = captchaEnabled;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public T getData() {
return data;
@@ -1,30 +0,0 @@
package com.dahe.mylibrary.net
import java.io.Serializable
/**
* 返回通用的实体类
*/
class CommonResponseBean2<T> : Serializable {
var data : T? = null
var code = 0
var msg: String? = null
// 数据未封装 无奈呀
var isCaptchaEnabled = false
var img: String? = null
var uuid: String? = null
var total = 0
var rows: T? = null
private set
fun setRows(rows: T) {
this.rows = rows
}
fun setData(data: T) {
this.data = data
}
}
@@ -4,13 +4,19 @@ import android.text.TextUtils;
import android.util.Log;
import com.google.zxing.common.StringUtils;
import com.google.gson.JsonNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.spec.KeySpec;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import okhttp3.Interceptor;
import okhttp3.MediaType;
@@ -45,16 +51,34 @@ public class JsonInterceptor implements Interceptor {
Log.i(TAG, "result-body= " + responseBodyStr);
try {
JSONObject jsonObject = new JSONObject(responseBodyStr);
String str = jsonObject.optString("data");
int code = jsonObject.optInt("code");
// if (-1 == code) {
// jsonObject.put("data", new JSONObject());
// throw new ResultException();
// } else {
// if (TextUtils.isEmpty(str)) {
// jsonObject.put("data", new JSONObject());
// }
// }
boolean hasData = jsonObject.has("data");
if (!hasData) {
//兼容后台接口,重新封装一下data
JSONObject toData = new JSONObject();
Iterator<String> keys = jsonObject.keys();
// 标记需要删除的key
ArrayList<String> strings = new ArrayList<>();
while (keys.hasNext()) {
String key = keys.next();
if ("code".equals(key) || "msg".equals(key) ) {
continue;
}
Object o = jsonObject.get(key);
if (o instanceof JSONObject) {
toData.putOpt(key, new JSONObject(o.toString()));
} else if (o instanceof JSONArray) {
toData.putOpt(key, new JSONArray(o.toString()));
}else {
toData.putOpt(key, o);
}
strings.add(key);
}
for (String s : strings) {
jsonObject.remove(s);
}
jsonObject.put("data", toData);
}
String data = jsonObject.toString();
responseBody = ResponseBody.create(MediaType.parse("application/json; charset=utf-8"), data);