Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用中问题和解决方法记录 #48

Open
guodun123 opened this issue Aug 25, 2023 · 0 comments
Open

使用中问题和解决方法记录 #48

guodun123 opened this issue Aug 25, 2023 · 0 comments

Comments

@guodun123
Copy link

guodun123 commented Aug 25, 2023

自定义Result

可能遇到的问题:

  1. ApiResult.class.isAssignableFrom(cls)err!! 原因类被混淆了 需要添加 -keep class 路径
  2. 返回code为不是int类型;
    解决当然是 新建一个枚举映射类啊HttpResponseCode
public class CustomApiResult<T> extends ApiResult<T> {

    private String errorCode;
    private String errorMessage;
    private String requestId;
    private Boolean success;
    private String timestamp;

     //省略get set....

    @Override
    public int getCode() {
        return HttpResponseCode.transServerCode(errorCode);
    }

    @Override
    public String getMsg() {
        return errorMessage;
    }

    @Override
    public boolean isSuccess() {
        return success;
    }

    @Override
    public CustomApiResult setData(T data) {
        super.setData(data);
        return this;
    }

    @Override
    public T getData() {
        return super.getData();
    }

    @Override
    public String toString() {
        return "CustomApiResult{" +
                "code='" + super.getCode() + '\'' +
                ", message='" + super.getMsg() + '\'' +
                ", result=" + super.getData() +
                '}';
    }
}




public enum HttpResponseCode {
    //成功code
    SUCCESS_CODE("0000", 200),
    //未绑定店铺
    NO_SHOP_ERROR_CODE("A0001", 201),
    //需要验证码
    NEED_LOGIN_CODE("A0211", 202);

    HttpResponseCode(String serverCode, int pdaCode) {
        this.serverCode = serverCode;
        this.pdaCode = pdaCode;
    }

    private String serverCode;
    private int pdaCode;

    public String getServerCode() {
        return serverCode;
    }

    public int getPdaCode() {
        return pdaCode;
    }

    public static int transServerCode(String serverCode) {
        for (HttpResponseCode code : HttpResponseCode.values()) {
            if (code.getServerCode().equals(serverCode)) {
                return code.getPdaCode();
            }
        }
        return 0;
    }
}

重要事情说三遍

别忘了 添加 : -keep class CustomApiResult 路径

别忘了 添加 : -keep class CustomApiResult 路径

别忘了 添加 : -keep class CustomApiResult 路径

返回 的Data 不是一个JsonObject是String Long 等类型

{
    "errorCode": "00000",
    "errorMessage": null,
    "data": "rsapublckkey",
    "requestId": "xfdf22121s",
    "success": true
}




//此处以String为例子
XHttp.get("/web/auth/getEncryptKey")
			.execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
				@Override
				public void onSuccess(String responseString) throws Throwable {
				//这里的responseString 为 上面的整个后台返回报文
				ApiResponse response = GSONUtil.parseStringData(responseString, ApiResponse.class);
				//然后  类型自己转 判空啥的 注意
				String a = response.getData(),toSting();
				}
			}){});  //千万注意,这里的{}一定不能去掉,否则解析错误


//lombok插件
@Data
public class ApiResponse implements Serializable {
    private String errorCode;
    private String errorMessage;
    private String requestId;
    private Boolean success;
    private Object data;
 
}

动态添加参数的拦截器遇到的坑

添加token 时当token 包含特殊字符 如 空格 等 会转义导致后台校验token失败
解决:不使用拦截器添加;在XHttp.get("") 或者XHttp.post("")是添加Header;
在自己项目里 对 XHttp.get("") ;post等请求 再封装一层,在这里添加Header 等其他操作

Cookie

    XHttp.getInstance().setCookieStore(CookieManager.getInstance(application));

反正我这一行代码就解决 了;

其他走代理的请求 没用过 没试过;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant