我有其他人编写的 Json 解析器类。我闻到这种检查器方法不好:
public boolean isCorrectResponse() {
try {
if (localResponse != null) {
JSONObject jResponse = new JSONObject(localResponse);
if (jResponse.get("result") instanceof JSONObject) {
JSONObject jResult = jResponse.getJSONObject("result");
if (jResult.get("error") instanceof JSONArray) {
JSONObject jError = jResult.getJSONArray("error").getJSONObject(0);
if (!jError.toString().equals("")) {
String errorMsg = jError.getString("msg");
String errorCode = jError.getString("code");
showErrorMessage(errorCode + "; " + errorMsg);
return false;
}
}
}
} else {
return false;
}
} catch (JSONException e) {
L.e("ERROR: on isCorrectResponse method!");
e.printStackTrace();
//return false; //Added myself Google Json should throw error shouldn't it??? Which means response was wrong???
}
return true;
}
不应该在第一次尝试创建时抛出错误
JSONObject jResponse = new JSONObject(localResponse);
并且一切都会立即解决(我只需要返回false)?是否需要在 try 正文中进行这些额外检查?我正在使用 Google Gson 库来解析 Json 并为 Android 开发。