Java Code Examples for com.facebook.internal.Utility#getStringPropertyAsJSON()

The following examples show how to use com.facebook.internal.Utility#getStringPropertyAsJSON() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Response.java    From Abelana-Android with Apache License 2.0 5 votes vote down vote up
private static Response createResponseFromObject(Request request, HttpURLConnection connection, Object object,
        boolean isFromCache, Object originalResult) throws JSONException {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;

        FacebookRequestError error =
                FacebookRequestError.checkResponseAndCreateError(jsonObject, originalResult, connection);
        if (error != null) {
            if (error.getErrorCode() == INVALID_SESSION_FACEBOOK_ERROR_CODE) {
                Session session = request.getSession();
                if (session != null) {
                    session.closeAndClearTokenInformation();
                }
            }
            return new Response(request, connection, error);
        }

        Object body = Utility.getStringPropertyAsJSON(jsonObject, BODY_KEY, NON_JSON_RESPONSE_PROPERTY);

        if (body instanceof JSONObject) {
            GraphObject graphObject = GraphObject.Factory.create((JSONObject) body);
            return new Response(request, connection, body.toString(), graphObject, isFromCache);
        } else if (body instanceof JSONArray) {
            GraphObjectList<GraphObject> graphObjectList = GraphObject.Factory.createList(
                    (JSONArray) body, GraphObject.class);
            return new Response(request, connection, body.toString(), graphObjectList, isFromCache);
        }
        // We didn't get a body we understand how to handle, so pretend we got nothing.
        object = JSONObject.NULL;
    }

    if (object == JSONObject.NULL) {
        return new Response(request, connection, object.toString(), (GraphObject)null, isFromCache);
    } else {
        throw new FacebookException("Got unexpected object type in response, class: "
                + object.getClass().getSimpleName());
    }
}
 
Example 2
Source File: Response.java    From facebook-api-android-maven with Apache License 2.0 5 votes vote down vote up
private static Response createResponseFromObject(Request request, HttpURLConnection connection, Object object,
        boolean isFromCache, Object originalResult) throws JSONException {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;

        FacebookRequestError error =
                FacebookRequestError.checkResponseAndCreateError(jsonObject, originalResult, connection);
        if (error != null) {
            if (error.getErrorCode() == INVALID_SESSION_FACEBOOK_ERROR_CODE) {
                Session session = request.getSession();
                if (session != null) {
                    session.closeAndClearTokenInformation();
                }
            }
            return new Response(request, connection, error);
        }

        Object body = Utility.getStringPropertyAsJSON(jsonObject, BODY_KEY, NON_JSON_RESPONSE_PROPERTY);

        if (body instanceof JSONObject) {
            GraphObject graphObject = GraphObject.Factory.create((JSONObject) body);
            return new Response(request, connection, body.toString(), graphObject, isFromCache);
        } else if (body instanceof JSONArray) {
            GraphObjectList<GraphObject> graphObjectList = GraphObject.Factory.createList(
                    (JSONArray) body, GraphObject.class);
            return new Response(request, connection, body.toString(), graphObjectList, isFromCache);
        }
        // We didn't get a body we understand how to handle, so pretend we got nothing.
        object = JSONObject.NULL;
    }

    if (object == JSONObject.NULL) {
        return new Response(request, connection, object.toString(), (GraphObject)null, isFromCache);
    } else {
        throw new FacebookException("Got unexpected object type in response, class: "
                + object.getClass().getSimpleName());
    }
}
 
Example 3
Source File: FacebookRequestError.java    From kognitivo with Apache License 2.0 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(
        JSONObject singleResult,
        Object batchResult,
        HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    GraphResponse.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either
                // an "error" with several sub-properties, or else one or more top-level fields
                // containing error info.
                String errorType = null;
                String errorMessage = null;
                String errorUserMessage = null;
                String errorUserTitle = null;
                boolean errorIsTransient = false;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject)
                            Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    errorUserMessage =  error.optString(ERROR_USER_MSG_KEY, null);
                    errorUserTitle =  error.optString(ERROR_USER_TITLE_KEY, null);
                    errorIsTransient = error.optBoolean(ERROR_IS_TRANSIENT_KEY, false);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(
                            responseCode,
                            errorCode,
                            errorSubCode,
                            errorType,
                            errorMessage,
                            errorUserTitle,
                            errorUserMessage,
                            errorIsTransient,
                            jsonBody,
                            singleResult,
                            batchResult,
                            connection,
                            null);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report
            // it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(
                        responseCode,
                        INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE,
                        null,
                        null,
                        null,
                        null,
                        false,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult,
                                        BODY_KEY,
                                        GraphResponse.NON_JSON_RESPONSE_PROPERTY
                                ) : null,
                        singleResult,
                        batchResult,
                        connection,
                        null);
            }
        }
    } catch (JSONException e) {
    }
    return null;
}
 
Example 4
Source File: GraphResponse.java    From kognitivo with Apache License 2.0 4 votes vote down vote up
private static GraphResponse createResponseFromObject(
        GraphRequest request,
        HttpURLConnection connection,
        Object object,
        Object originalResult
) throws JSONException {
    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;

        FacebookRequestError error =
                FacebookRequestError.checkResponseAndCreateError(
                        jsonObject,
                        originalResult,
                        connection);
        if (error != null) {
            if (error.getErrorCode() == FacebookRequestErrorClassification.EC_INVALID_TOKEN
                    && Utility.isCurrentAccessToken(request.getAccessToken())) {
                AccessToken.setCurrentAccessToken(null);
            }
            return new GraphResponse(request, connection, error);
        }

        Object body = Utility.getStringPropertyAsJSON(
                jsonObject,
                BODY_KEY,
                NON_JSON_RESPONSE_PROPERTY);

        if (body instanceof JSONObject) {
            return new GraphResponse(request, connection, body.toString(), (JSONObject)body);
        } else if (body instanceof JSONArray) {
            return new GraphResponse(request, connection, body.toString(), (JSONArray)body);
        }
        // We didn't get a body we understand how to handle, so pretend we got nothing.
        object = JSONObject.NULL;
    }

    if (object == JSONObject.NULL) {
        return new GraphResponse(request, connection, object.toString(), (JSONObject)null);
    } else {
        throw new FacebookException("Got unexpected object type in response, class: "
                + object.getClass().getSimpleName());
    }
}
 
Example 5
Source File: FacebookRequestError.java    From platform-friends-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 6
Source File: FacebookRequestError.java    From Klyph with MIT License 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 7
Source File: FacebookRequestError.java    From barterli_android with Apache License 2.0 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 8
Source File: FacebookRequestError.java    From android-skeleton-project with MIT License 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 9
Source File: FacebookRequestError.java    From FacebookImageShareIntent with MIT License 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 10
Source File: FacebookRequestError.java    From aws-mobile-self-paced-labs-samples with Apache License 2.0 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 11
Source File: FacebookRequestError.java    From Abelana-Android with Apache License 2.0 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                String errorUserMessage = null;
                String errorUserTitle = null;
                boolean errorIsTransient = false;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    errorUserMessage =  error.optString(ERROR_USER_MSG_KEY, null);
                    errorUserTitle =  error.optString(ERROR_USER_TITLE_KEY, null);
                    errorIsTransient = error.optBoolean(ERROR_IS_TRANSIENT_KEY, false);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, errorUserTitle, errorUserMessage, errorIsTransient, jsonBody,
                            singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null, null, null, false,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 12
Source File: FacebookRequestError.java    From KlyphMessenger with MIT License 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 13
Source File: FacebookRequestError.java    From facebook-api-android-maven with Apache License 2.0 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                String errorUserMessage = null;
                String errorUserTitle = null;
                boolean errorIsTransient = false;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    errorUserMessage =  error.optString(ERROR_USER_MSG_KEY, null);
                    errorUserTitle =  error.optString(ERROR_USER_TITLE_KEY, null);
                    errorIsTransient = error.optBoolean(ERROR_IS_TRANSIENT_KEY, false);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, errorUserTitle, errorUserMessage, errorIsTransient, jsonBody,
                            singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null, null, null, false,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 14
Source File: FacebookRequestError.java    From HypFacebook with BSD 2-Clause "Simplified" License 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}
 
Example 15
Source File: FacebookRequestError.java    From FacebookNewsfeedSample-Android with Apache License 2.0 4 votes vote down vote up
static FacebookRequestError checkResponseAndCreateError(JSONObject singleResult,
        Object batchResult, HttpURLConnection connection) {
    try {
        if (singleResult.has(CODE_KEY)) {
            int responseCode = singleResult.getInt(CODE_KEY);
            Object body = Utility.getStringPropertyAsJSON(singleResult, BODY_KEY,
                    Response.NON_JSON_RESPONSE_PROPERTY);

            if (body != null && body instanceof JSONObject) {
                JSONObject jsonBody = (JSONObject) body;
                // Does this response represent an error from the service? We might get either an "error"
                // with several sub-properties, or else one or more top-level fields containing error info.
                String errorType = null;
                String errorMessage = null;
                int errorCode = INVALID_ERROR_CODE;
                int errorSubCode = INVALID_ERROR_CODE;

                boolean hasError = false;
                if (jsonBody.has(ERROR_KEY)) {
                    // We assume the error object is correctly formatted.
                    JSONObject error = (JSONObject) Utility.getStringPropertyAsJSON(jsonBody, ERROR_KEY, null);

                    errorType = error.optString(ERROR_TYPE_FIELD_KEY, null);
                    errorMessage = error.optString(ERROR_MESSAGE_FIELD_KEY, null);
                    errorCode = error.optInt(ERROR_CODE_FIELD_KEY, INVALID_ERROR_CODE);
                    errorSubCode = error.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                } else if (jsonBody.has(ERROR_CODE_KEY) || jsonBody.has(ERROR_MSG_KEY)
                        || jsonBody.has(ERROR_REASON_KEY)) {
                    errorType = jsonBody.optString(ERROR_REASON_KEY, null);
                    errorMessage = jsonBody.optString(ERROR_MSG_KEY, null);
                    errorCode = jsonBody.optInt(ERROR_CODE_KEY, INVALID_ERROR_CODE);
                    errorSubCode = jsonBody.optInt(ERROR_SUB_CODE_KEY, INVALID_ERROR_CODE);
                    hasError = true;
                }

                if (hasError) {
                    return new FacebookRequestError(responseCode, errorCode, errorSubCode,
                            errorType, errorMessage, jsonBody, singleResult, batchResult, connection);
                }
            }

            // If we didn't get error details, but we did get a failure response code, report it.
            if (!HTTP_RANGE_SUCCESS.contains(responseCode)) {
                return new FacebookRequestError(responseCode, INVALID_ERROR_CODE,
                        INVALID_ERROR_CODE, null, null,
                        singleResult.has(BODY_KEY) ?
                                (JSONObject) Utility.getStringPropertyAsJSON(
                                        singleResult, BODY_KEY, Response.NON_JSON_RESPONSE_PROPERTY) : null,
                        singleResult, batchResult, connection);
            }
        }
    } catch (JSONException e) {
        // defer the throwing of a JSONException to the graph object proxy
    }
    return null;
}