從它的父類方法退出子類方法執行給定下面的代碼片段:如何停止/ 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」)'' –
這是因爲我的服務器將始終包含「狀態」鍵,狀態可以等於成功或失敗。所以,我需要知道是成功還是失敗。 –
但是,只有在沒有「狀態」鍵的情況下才會觸發您正在捕捉的異常。 –