概括了例外处理Retrofit

时间:2016-06-06 15:09:26

标签: android design-patterns retrofit retrofit2

我的Retrofit服务中有多个API调用,每个方法都处理相同的丑陋异常并检查JSON响应中的相同属性。我试图让它更通用,以减少每种方法的大小,但我不知道最好的方法是什么...这是许多方法中的两个做同样的事情,但使用不同的API调用:

private void sendStopSecurityCheck() {
    Intent intent = new Intent(STOP_SECURITY_CHECK);
    intent.putExtra(RESULT, RESULT_FAILED);

    boolean isSuccess = connectAmazonApi();

    if(isSuccess){
        Call<ResponseBody> call = amazonServices.stopSecurityCheck(settings.getString("username", ""), settings.getString("orgid", ""));
        try {
            ResponseBody response = call.execute().body();
            JSONObject obj;
            if (response != null) {
                obj = new JSONObject(response.string());
                if(obj.getBoolean("success") == true){
                    intent.putExtra(RESULT, RESULT_OK);
                }
                Log.w(TAG, "Result " + obj.toString());
            }
        } catch (IOException e) {
            Log.w(TAG, "Request failed: " + e.getMessage());
        } catch (JSONException e) {
            Log.w(TAG, "Request failed: " + e.getMessage());
        }
    }else{
        Log.w(TAG, "Impossible to connect to Amazon API");
    }

    sendBroadcast(intent);
}

private void sendConfirmSecurityCheck() {
    Intent intent = new Intent(CONFIRM_SECURITY_CHECK);
    intent.putExtra(RESULT, RESULT_FAILED);

    boolean isSuccess = connectAmazonApi();

    if(isSuccess){
        Call<ResponseBody> call = amazonServices.confirmSecurityCheck(settings.getString("username", ""), settings.getString("orgid", ""));
        try {
            ResponseBody response = call.execute().body();
            JSONObject obj;
            if (response != null) {
                obj = new JSONObject(response.string());
                if(obj.getBoolean("success") == true){
                    intent.putExtra(RESULT, RESULT_OK);
                }
                Log.w(TAG, "Result " + obj.toString());
            }
        } catch (IOException e) {
            Log.w(TAG, "Request failed: " + e.getMessage());
        } catch (JSONException e) {
            Log.w(TAG, "Request failed: " + e.getMessage());
        }
    }else{
        Log.w(TAG, "Impossible to connect to Amazon API");
    }

    sendBroadcast(intent);
}

1 个答案:

答案 0 :(得分:1)

如何通过提取两种方法来重构代码?

封装同步请求调用的一种方法,另一种检查同步请求是否成功的方法。

private ResponseBody executeSynchronously(Call<ResponseBody> call) {
    try {
        return call.execute().body();
    } catch (IOException e) {
        Log.w(TAG, "Request failed: " + e.getMessage());
    }
    return null;
}

private boolean parseResponse(ResponseBody response) {
    if (response == null) return false;
    JSONObject obj = new JSONObject(response.string());
    try {
        Log.w(TAG, "Result " + obj.toString());
        return obj.getBoolean("success") == true;
    } catch (JSONException e) {
        Log.w(TAG, "Request failed: " + e.getMessage());
    }
    return false;
}

现在重构sendConfirmSecurityCheck方法,如下所示:

private void sendConfirmSecurityCheck() {
    Intent intent = new Intent(CONFIRM_SECURITY_CHECK);
    intent.putExtra(RESULT, RESULT_FAILED);

    boolean isSuccess = connectAmazonApi();

    if(isSuccess){
        Call<ResponseBody> call = amazonServices.confirmSecurityCheck(settings.getString("username", ""), settings.getString("orgid", ""));
        ResponseBody response = executeSynchronously(call);
        if (parseResponse(response)) {
            intent.putExtra(RESULT, RESULT_OK);
        }
    } else {
        Log.w(TAG, "Impossible to connect to Amazon API");
    }

    sendBroadcast(intent);
}