添加接口统一拦截码3800 跳登录页面
This commit is contained in:
@@ -521,7 +521,11 @@ public abstract class BaseActivity extends AppCompatActivity implements
|
||||
Handler mHandler = new Handler() {
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg != null) {
|
||||
String resultData = (String) msg.obj;
|
||||
String resultData = (String) msg.obj;
|
||||
if (UIUtils.interceptCode38000(ct, resultData)) {
|
||||
closeProgressDialog();
|
||||
return;
|
||||
}
|
||||
OnCallBackFromNet callBack = (OnCallBackFromNet) msg.getData().getSerializable("callback");
|
||||
int i = msg.what;
|
||||
if (callBack == null) {
|
||||
|
||||
@@ -242,7 +242,11 @@ public abstract class BaseFragment extends Fragment {
|
||||
Handler mHandler = new Handler() {
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg != null) {
|
||||
String resultData = (String) msg.obj;
|
||||
String resultData = (String) msg.obj;
|
||||
if (UIUtils.interceptCode38000(getActivity(), resultData)) {
|
||||
closeProgressDialog();
|
||||
return;
|
||||
}
|
||||
OnCallBackFromNet callBack = (OnCallBackFromNet) msg.getData().getSerializable("callback");
|
||||
int i = msg.what;
|
||||
if (callBack == null) {
|
||||
|
||||
@@ -28,7 +28,10 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
|
||||
import cn.shangyu.gdxzExpert.utils.IoUtils;
|
||||
import cn.shangyu.gdxzExpert.utils.StringUtil;
|
||||
import cn.shangyu.gdxzExpert.utils.StringUtil;
|
||||
import cn.shangyu.gdxzExpert.utils.UIUtils;
|
||||
import com.lidroid.xutils.exception.HttpException;
|
||||
import com.lidroid.xutils.http.ResponseInfo;
|
||||
|
||||
public class HttpHelper {
|
||||
// public static final String URL = "http://10.0.2.2:8080/";
|
||||
@@ -117,7 +120,37 @@ public class HttpHelper {
|
||||
// else {
|
||||
// http.send(method, url, params, callback);
|
||||
// }
|
||||
HttpHandler<String> cancleHttp = http.send(method, url, params,callback);
|
||||
final RequestCallBack<String> finalCallback = callback;
|
||||
RequestCallBack<String> wrappedCallback = finalCallback == null ? null : new RequestCallBack<String>() {
|
||||
@Override
|
||||
public void onStart() {
|
||||
finalCallback.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoading(long total, long current, boolean isUploading) {
|
||||
finalCallback.onLoading(total, current, isUploading);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled() {
|
||||
finalCallback.onCancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(ResponseInfo<String> responseInfo) {
|
||||
if (responseInfo != null && UIUtils.interceptCode38000(UIUtils.getContext(), responseInfo.result)) {
|
||||
return;
|
||||
}
|
||||
finalCallback.onSuccess(responseInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException error, String msg) {
|
||||
finalCallback.onFailure(error, msg);
|
||||
}
|
||||
};
|
||||
HttpHandler<String> cancleHttp = http.send(method, url, params, wrappedCallback);
|
||||
// if(cancleHttp!=null){
|
||||
// cancleHttp.cancel();//取消网络请求
|
||||
// }
|
||||
@@ -277,7 +310,8 @@ public class HttpHelper {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
byte[] data = out.toByteArray();
|
||||
mStr = new String(data, "utf-8");
|
||||
mStr = new String(data, "utf-8");
|
||||
UIUtils.interceptCode38000(UIUtils.getContext(), mStr);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
||||
@@ -81,7 +81,10 @@ public class HttpMD5SendInfoUtil {
|
||||
if (conn.getResponseCode() == 200) {
|
||||
InputStream inStream = conn.getInputStream();
|
||||
String result = new String(StreamTool.readInputStream(inStream), "UTF-8");
|
||||
String string = new String(result);
|
||||
String string = new String(result);
|
||||
if (UIUtils.interceptCode38000(getContext(), string)) {
|
||||
return;
|
||||
}
|
||||
JSONObject jsonObject = new JSONObject(string);
|
||||
String optString = jsonObject.optString("code");
|
||||
String message = jsonObject.optString("message");
|
||||
@@ -170,7 +173,10 @@ public class HttpMD5SendInfoUtil {
|
||||
if (ssss == 200) {
|
||||
InputStream inStream = conn.getInputStream();
|
||||
String result = new String(StreamTool.readInputStream(inStream), "UTF-8");
|
||||
String string = new String(result);
|
||||
String string = new String(result);
|
||||
if (UIUtils.interceptCode38000(getContext(), string)) {
|
||||
return;
|
||||
}
|
||||
LogUtil.i("result = "+result);
|
||||
JSONObject jsonObject = new JSONObject(string);
|
||||
String optString = jsonObject.optString("code");
|
||||
@@ -260,7 +266,10 @@ public class HttpMD5SendInfoUtil {
|
||||
if (ssss == 200) {
|
||||
InputStream inStream = conn.getInputStream();
|
||||
String result = new String(StreamTool.readInputStream(inStream), "UTF-8");
|
||||
String string = new String(result);
|
||||
String string = new String(result);
|
||||
if (UIUtils.interceptCode38000(getContext(), string)) {
|
||||
return;
|
||||
}
|
||||
LogUtil.i("result = "+result);
|
||||
JSONObject jsonObject = new JSONObject(string);
|
||||
String optString = jsonObject.optString("code");
|
||||
@@ -345,7 +354,10 @@ public class HttpMD5SendInfoUtil {
|
||||
if (ssss == 200) {
|
||||
InputStream inStream = conn.getInputStream();
|
||||
String result = new String(StreamTool.readInputStream(inStream), "UTF-8");
|
||||
String string = new String(result);
|
||||
String string = new String(result);
|
||||
if (UIUtils.interceptCode38000(getContext(), string)) {
|
||||
return;
|
||||
}
|
||||
LogUtil.i("result = "+result);
|
||||
JSONObject jsonObject = new JSONObject(string);
|
||||
String optString = jsonObject.optString("code");
|
||||
|
||||
@@ -375,6 +375,64 @@ public class UIUtils {
|
||||
*/
|
||||
private static long lastJumpLoginTime = 0L;
|
||||
|
||||
/**
|
||||
* 全局拦截网络请求返回码 code == 38000,清理失效登录态并跳转登录页
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param resultJson 接口返回的 JSON 字符串
|
||||
* @return true 表示命中 code == 38000 并已拦截跳转;false 表示正常放行
|
||||
*/
|
||||
public static boolean interceptCode38000(final Context context, String resultJson) {
|
||||
if (StringUtil.isEmpty(resultJson) || !resultJson.contains("38000")) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
org.json.JSONObject jsonObject = new org.json.JSONObject(resultJson);
|
||||
String code = jsonObject.optString("code");
|
||||
if (!"38000".equals(code) && jsonObject.optInt("code", -1) != 38000) {
|
||||
return false;
|
||||
}
|
||||
runInMainThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Context appCtx = getContext();
|
||||
if (appCtx != null && isAccountLogined()) {
|
||||
SharePrefUtil.saveBoolean(appCtx, Constant.IS_LOGIN, false);
|
||||
SharePrefUtil.saveString(appCtx, Constant.uuid, "");
|
||||
SharePrefUtil.saveString(appCtx, Constant.USERID, "");
|
||||
SharePrefUtil.saveString(appCtx, "access_token", "");
|
||||
SharePrefUtil.saveString(appCtx, "refresh_token", "");
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - lastJumpLoginTime < 1500) {
|
||||
return;
|
||||
}
|
||||
Activity topActivity = cn.shangyu.gdxzExpert.base.AppManager.getAppManager().currentActivity();
|
||||
if (topActivity instanceof LoginNewActivity
|
||||
|| topActivity instanceof cn.shangyu.gdxzExpert.activity.LoginActivity
|
||||
|| topActivity instanceof cn.shangyu.gdxzExpert.activity.BindNumberActivity
|
||||
|| topActivity instanceof cn.shangyu.gdxzExpert.activity.RegistActivity) {
|
||||
return;
|
||||
}
|
||||
lastJumpLoginTime = now;
|
||||
Context targetContext = context != null ? context : (topActivity != null ? topActivity : appCtx);
|
||||
if (targetContext == null) {
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(targetContext, LoginNewActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
if (!(targetContext instanceof Activity)) {
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
}
|
||||
targetContext.startActivity(intent);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean checkLoginAndJump(Context context) {
|
||||
if (isLogined()) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user