package com.bigdo.util;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.bigdo.app.BaseActivity;
import com.bigdo.common.RState;
import android.os.Message;
public final class WSCheck {
public static boolean Call(BaseActivity a, Message msg) {
StringBuilder outMsg = new StringBuilder();
boolean isOk = Call(a, msg, outMsg);
if (!isOk && outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
return isOk;
}
public static boolean Call(BaseActivity a, Message msg, StringBuilder outMsg) {
boolean isOk = false;
if (msg.arg1 == RState.WSCall) {
WSResult result = (WSResult) msg.getData().getParcelable(
WSResult.ResultKey);
isOk = Call(a, result, outMsg);
result = null;
}
return isOk;
}
public static boolean Call(BaseActivity a, WSResult result) {
boolean isOk = false;
StringBuilder outMsg = new StringBuilder();
isOk = Call(a, result, outMsg);
if (!isOk && outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
result = null;
return isOk;
}
public static boolean Call(BaseActivity a, WSResult result,
StringBuilder outMsg) {
boolean isOk = false;
if (result != null) {
if (result.State == WSResult.OK) {
isOk = true;
} else if (result.State == WSResult.Error) {
if (outMsg != null) {
outMsg.append("亲! 服务错误了哦~");
}
} else if (result.State == WSResult.UnknownHost) {
if (outMsg != null) {
outMsg.append("亲!服务可能搬家 或 休眠中了~");
}
} else if (result.State == WSResult.ServiceBreak) {
if (outMsg != null) {
outMsg.append("亲!您的网络不太好,服务中断了~");
}
} else if (result.State == WSResult.NetTimeout) {
if (outMsg != null) {
outMsg.append("亲!您的网络不太好~");
}
} else if (result.State == WSResult.NetError) {
if (outMsg != null) {
outMsg.append("亲!您的网络不太好~");
}
} else if (result.State == WSResult.SError404) {
if (outMsg != null) {
outMsg.append("亲!服务可能搬家 或 休眠中了~");
}
} else if (result.State == WSResult.SError) {
if (outMsg != null) {
outMsg.append("亲!服务错误了哦~");
}
} else {
if (outMsg != null) {
outMsg.append("亲!检测到了外星人的攻击~");
}
}
}
return isOk;
}
public static JSONObject Json(BaseActivity a, Message msg) {
StringBuilder outMsg = new StringBuilder();
JSONObject jresult = Json(a, msg, outMsg);
if (jresult == null && outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
return jresult;
}
/**
* 检查WS调用
*
* @param msg
* @return
*/
public static JSONObject Json(BaseActivity a, Message msg,
StringBuilder outMsg) {
JSONObject jresult = null;
if (msg.arg1 == RState.WSCall) {
WSResult result = (WSResult) msg.getData().getParcelable(
WSResult.ResultKey);
jresult = Json(a, result, outMsg);
result = null;
}
return jresult;
}
/**
* 检查WS调用
*
* @param msg
* @return
*/
public static JSONObject Json(BaseActivity a, WSResult result) {
StringBuilder outMsg = new StringBuilder();
JSONObject jresult = Json(a, result, outMsg);
if (jresult == null && outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
return jresult;
}
/**
* 检查WS调用
*
* @param msg
* @return
*/
public static JSONObject Json(BaseActivity a, WSResult result,
StringBuilder outMsg) {
JSONObject jresult = null;
if (result != null) {
boolean isOk = Call(a, result, outMsg);
if (isOk) {
jresult = Json(a, result.Result, outMsg);
}
}
return jresult;
}
public static JSONObject Json(BaseActivity a, final String json) {
StringBuilder outMsg = new StringBuilder();
JSONObject jresult = Json(a, json, outMsg);
if (jresult == null && outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
return jresult;
}
public static JSONObject Json(BaseActivity a, final String json,
StringBuilder outMsg) {
JSONObject jresult = null;
if (json != null && !json.equals("")) {
try {
JSONTokener JSONTokener = new JSONTokener(json);
jresult = new JSONObject(JSONTokener);
int __status = jresult.getInt("__status");
int __result = jresult.getInt("__result");
String __stateInfo = jresult.getString("__stateInfo");
if (Result(a, __status, __result, __stateInfo, outMsg)) {
if (outMsg != null) {
outMsg.delete(0, outMsg.length());
}
} else {
jresult = null;
}
// jparams = null;
JSONTokener = null;
} catch (Exception ex) {
jresult = null;
if (outMsg != null) {
outMsg.append("抱歉!服务返回的数据有误 或 出错了.");
}
}
}
return jresult;
}
public static JSONObject Json(BaseActivity a, final JSONObject jresult) {
StringBuilder outMsg = new StringBuilder();
if (jresult != null) {
try {
int __status = jresult.getInt("__status");
int __result = jresult.getInt("__result");
String __stateInfo = jresult.getString("__stateInfo");
if (Result(a, __status, __result, __stateInfo, outMsg)) {
if (outMsg != null) {
outMsg.delete(0, outMsg.length());
}
}
} catch (Exception ex) {
if (outMsg != null) {
outMsg.append("抱歉!服务返回的数据有误 或 出错了.");
}
}
if (outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
}
return jresult;
}
public static JSONObject Json(BaseActivity a, final JSONObject jresult,
StringBuilder outMsg) {
if (jresult != null) {
try {
int __status = jresult.getInt("__status");
int __result = jresult.getInt("__result");
String __stateInfo = jresult.getString("__stateInfo");
if (Result(a, __status, __result, __stateInfo, outMsg)) {
if (outMsg != null) {
outMsg.delete(0, outMsg.length());
}
}
// jparams = null;
} catch (Exception ex) {
if (outMsg != null) {
outMsg.append("抱歉!服务返回的数据有误 或 出错了.");
}
}
}
return jresult;
}
public static boolean Result(BaseActivity a, int state, int result,
String stateInfo) {
StringBuilder outMsg = new StringBuilder();
boolean isOk = Result(a, state, result, stateInfo, outMsg);
if (!isOk && outMsg != null && outMsg.length() > 0) {
a.messageBox(outMsg.toString());
outMsg.delete(0, outMsg.length());
}
return isOk;
}
public static boolean Result(BaseActivity a, int state, int result,
String stateInfo, StringBuilder outMsg) {
boolean isOk = false;
if (state == WSResult.SOK) {
if (result == WSResult.ROK) {
isOk = true;
} else if (result == WSResult.RLogOut) {
// 需要重新登陆
// LoginInfo.Clear(context);
isOk = false;
} else if (result == WSResult.RInParamError) {
// 输入参数错误
isOk = false;
if (outMsg != null) {
outMsg.append(stateInfo);
}
} else if (result == WSResult.RError) {
// 错误
isOk = false;
if (outMsg != null) {
outMsg.append(stateInfo);
}
} else {
isOk = false;
if (outMsg != null) {
if (stateInfo == null || stateInfo.equals("")) {
stateInfo = "亲!服务发生错误了哦.";
}
outMsg.append(stateInfo);
}
}
} else {
isOk = false;
if (outMsg != null) {
if (stateInfo == null || stateInfo.equals("")) {
stateInfo = "亲!服务发生错误了哦.";
}
outMsg.append(stateInfo);
}
}
return isOk;
}
}