从它的父类方法退出子类方法执行给定下面的代码片段:如何停止/ java中
超类的实现:
@Override
public void onDone(String taskName, JSONObject receivedData, @Nullable HashMap<String, String> sentData) {
//checks the status for all done process decides to call the presenter failed or success
try {
boolean success = receivedData.getString("status").equals("success");
if(!success){
this.presenter.error(receivedData.getString("reason"));
}
}catch (JSONException ex){
ex.printStackTrace();
this.presenter.error("An error occurred");
}
}
子类实现::
@Override
public void onDone(@NonNull String taskName, @NonNull JSONObject receivedData,
@Nullable HashMap<String, String> sentData) {
super.onDone(taskName, receivedData, sentData);
//the expected data has been received we should act upon it
//this DAO should know all the type of taskName it can handle and if it finds any that doesn't
//matches any of its command let it exit
Log.e(TAG, "Response : "+receivedData);
Toast.makeText(this.presenter.getContext(), "Done with task", Toast.LENGTH_LONG).show();
if(sentData!=null){
sentData.clear();
sentData = null;
}
}
我想要的是,只要super.onDone方法检测到错误,该过程应该结束,不应该打扰运行子类方法,在JAVA中可能吗?
无关的评论:为什么即使抓住'JSONException',你可以在访问它之前使用'receivedData.has(“status”)'' –
这是因为我的服务器将始终包含“状态”键,状态可以等于成功或失败。所以,我需要知道是成功还是失败。 –
但是,只有在没有“状态”键的情况下才会触发您正在捕捉的异常。 –