Search in sources :

Example 86 with JSONException

use of org.json.JSONException in project LiveSDK-for-Android by liveservices.

the class LiveConnectClient method postAsync.

/**
     * Performs an asynchronous HTTP POST on the Live Connect REST API.
     *
     * A POST adds a new resource to a collection.
     *
     * {@link LiveOperationListener#onComplete(LiveOperation)} will be called on success.
     * Otherwise, {@link LiveOperationListener#onError(LiveOperationException, LiveOperation)} will
     * be called. Both of these methods will be called on the main/UI thread.
     *
     * @param path object_id of the post request.
     * @param body body of the post request.
     * @param listener called on either completion or error during the copy request.
     * @param userState arbitrary object that is used to determine the caller of the method.
     * @return the LiveOperation associated with the request.
     * @throws IllegalArgumentException if the path is empty or is an absolute uri.
     * @throws NullPointerException if either the path or body parameters are null.
     */
public LiveOperation postAsync(String path, String body, LiveOperationListener listener, Object userState) {
    LiveConnectUtils.assertNotNullOrEmpty(body, ParamNames.BODY);
    JSONObject jsonBody;
    try {
        jsonBody = new JSONObject(body.toString());
    } catch (JSONException e) {
        return handleException(PostRequest.METHOD, path, new LiveOperationException(ErrorMessages.CLIENT_ERROR, e), listener, userState);
    }
    return this.postAsync(path, jsonBody, listener, userState);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 87 with JSONException

use of org.json.JSONException in project LiveSDK-for-Android by liveservices.

the class OAuthErrorResponse method createFromJson.

/**
     * Static constructor that creates an OAuthErrorResponse from the given OAuth server's
     * JSONObject response
     * @param response from the OAuth server
     * @return A new instance of an OAuthErrorResponse from the given response
     * @throws LiveAuthException if there is an JSONException, or the error type cannot be found.
     */
public static OAuthErrorResponse createFromJson(JSONObject response) throws LiveAuthException {
    final String errorString;
    try {
        errorString = response.getString(OAuth.ERROR);
    } catch (JSONException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    }
    final ErrorType error;
    try {
        error = ErrorType.valueOf(errorString.toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    } catch (NullPointerException e) {
        throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
    }
    final Builder builder = new Builder(error);
    if (response.has(OAuth.ERROR_DESCRIPTION)) {
        final String errorDescription;
        try {
            errorDescription = response.getString(OAuth.ERROR_DESCRIPTION);
        } catch (JSONException e) {
            throw new LiveAuthException(ErrorMessages.CLIENT_ERROR, e);
        }
        builder.errorDescription(errorDescription);
    }
    if (response.has(OAuth.ERROR_URI)) {
        final String errorUri;
        try {
            errorUri = response.getString(OAuth.ERROR_URI);
        } catch (JSONException e) {
            throw new LiveAuthException(ErrorMessages.CLIENT_ERROR, e);
        }
        builder.errorUri(errorUri);
    }
    return builder.build();
}
Also used : ErrorType(com.microsoft.live.OAuth.ErrorType) JSONException(org.json.JSONException)

Example 88 with JSONException

use of org.json.JSONException in project android-async-http by loopj.

the class JsonHttpResponseHandler method onFailure.

@Override
public final void onFailure(final int statusCode, final Header[] headers, final byte[] responseBytes, final Throwable throwable) {
    if (responseBytes != null) {
        Runnable parser = new Runnable() {

            @Override
            public void run() {
                try {
                    final Object jsonResponse = parseResponse(responseBytes);
                    postRunnable(new Runnable() {

                        @Override
                        public void run() {
                            // In RFC5179 a null value is not a valid JSON
                            if (!useRFC5179CompatibilityMode && jsonResponse == null) {
                                onFailure(statusCode, headers, (String) null, throwable);
                            } else if (jsonResponse instanceof JSONObject) {
                                onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
                            } else if (jsonResponse instanceof JSONArray) {
                                onFailure(statusCode, headers, throwable, (JSONArray) jsonResponse);
                            } else if (jsonResponse instanceof String) {
                                onFailure(statusCode, headers, (String) jsonResponse, throwable);
                            } else {
                                onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
                            }
                        }
                    });
                } catch (final JSONException ex) {
                    postRunnable(new Runnable() {

                        @Override
                        public void run() {
                            onFailure(statusCode, headers, ex, (JSONObject) null);
                        }
                    });
                }
            }
        };
        if (!getUseSynchronousMode() && !getUsePoolThread()) {
            new Thread(parser).start();
        } else {
            // In synchronous mode everything should be run on one thread
            parser.run();
        }
    } else {
        AsyncHttpClient.log.v(LOG_TAG, "response body is null, calling onFailure(Throwable, JSONObject)");
        onFailure(statusCode, headers, throwable, (JSONObject) null);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject)

Example 89 with JSONException

use of org.json.JSONException in project JsBridge by lzyzsd.

the class Message method toJson.

public String toJson() {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put(CALLBACK_ID_STR, getCallbackId());
        jsonObject.put(DATA_STR, getData());
        jsonObject.put(HANDLER_NAME_STR, getHandlerName());
        jsonObject.put(RESPONSE_DATA_STR, getResponseData());
        jsonObject.put(RESPONSE_ID_STR, getResponseId());
        return jsonObject.toString();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 90 with JSONException

use of org.json.JSONException in project JsBridge by lzyzsd.

the class Message method toArrayList.

public static List<Message> toArrayList(String jsonStr) {
    List<Message> list = new ArrayList<Message>();
    try {
        JSONArray jsonArray = new JSONArray(jsonStr);
        for (int i = 0; i < jsonArray.length(); i++) {
            Message m = new Message();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            m.setHandlerName(jsonObject.has(HANDLER_NAME_STR) ? jsonObject.getString(HANDLER_NAME_STR) : null);
            m.setCallbackId(jsonObject.has(CALLBACK_ID_STR) ? jsonObject.getString(CALLBACK_ID_STR) : null);
            m.setResponseData(jsonObject.has(RESPONSE_DATA_STR) ? jsonObject.getString(RESPONSE_DATA_STR) : null);
            m.setResponseId(jsonObject.has(RESPONSE_ID_STR) ? jsonObject.getString(RESPONSE_ID_STR) : null);
            m.setData(jsonObject.has(DATA_STR) ? jsonObject.getString(DATA_STR) : null);
            list.add(m);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return list;
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Aggregations

JSONException (org.json.JSONException)1904 JSONObject (org.json.JSONObject)1422 JSONArray (org.json.JSONArray)638 IOException (java.io.IOException)338 ArrayList (java.util.ArrayList)211 HashMap (java.util.HashMap)145 Test (org.junit.Test)128 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)98 File (java.io.File)77 Bundle (android.os.Bundle)75 VolleyError (com.android.volley.VolleyError)67 Intent (android.content.Intent)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)62 Response (com.android.volley.Response)59 URL (java.net.URL)57 Map (java.util.Map)48 TextView (android.widget.TextView)46 View (android.view.View)45 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 MalformedURLException (java.net.MalformedURLException)44