未捕获 JSONException
JSONException not caught
我有这段代码:
HJRestClient.post(path, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
JSONArray array = response.getJSONArray("tags");
if (adDetailViewAdapter != null) {
adDetailViewAdapter.setTags(array);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
它在 JSONArray array = response.getJSONArray("tags");
行崩溃,异常 W/System.err: org.json.JSONException: No value for tags
虽然我已经编写了捕获异常的代码,但它并没有被捕获并导致应用程序崩溃。
我有 2 台设备 Samsung Galaxy Note 5 Android v6.0.1
,其中 exception
被成功捕获。
另一个 Huawei Mate 7 Android v4.4.2
其中 exception
没有被捕获并导致崩溃。
我也写过:
catch (Exception exception) {}
但它仍然崩溃了。
有什么帮助吗?
你应该这样:
HJRestClient.post(path, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
if(response!=null&&response.has("tags")){
JSONArray array = response.getJSONArray("tags");
if (adDetailViewAdapter != null) {
adDetailViewAdapter.setTags(array);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
希望对您有所帮助。
我有这段代码:
HJRestClient.post(path, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
JSONArray array = response.getJSONArray("tags");
if (adDetailViewAdapter != null) {
adDetailViewAdapter.setTags(array);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
它在 JSONArray array = response.getJSONArray("tags");
行崩溃,异常 W/System.err: org.json.JSONException: No value for tags
虽然我已经编写了捕获异常的代码,但它并没有被捕获并导致应用程序崩溃。
我有 2 台设备 Samsung Galaxy Note 5 Android v6.0.1
,其中 exception
被成功捕获。
另一个 Huawei Mate 7 Android v4.4.2
其中 exception
没有被捕获并导致崩溃。
我也写过:
catch (Exception exception) {}
但它仍然崩溃了。
有什么帮助吗?
你应该这样:
HJRestClient.post(path, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
try {
if(response!=null&&response.has("tags")){
JSONArray array = response.getJSONArray("tags");
if (adDetailViewAdapter != null) {
adDetailViewAdapter.setTags(array);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
希望对您有所帮助。