Example usage for org.json JSONException JSONException

List of usage examples for org.json JSONException JSONException

Introduction

In this page you can find the example usage for org.json JSONException JSONException.

Prototype

public JSONException(Throwable t) 

Source Link

Usage

From source file:org.chromium.ChromeProxy.java

private void setHttpProxy(JSONObject rule, String prefix, String nonProxyHosts) throws JSONException {
    String scheme = rule.getString("scheme");
    if (!"http".equals(scheme)) {
        throw new JSONException("Scheme must be http");
    }//w  w w . j a  v a  2  s.  co m
    String host = rule.getString("host");
    int port = rule.has("port") ? rule.getInt("port") : 80;
    String portString = Integer.toString(port);
    System.setProperty(prefix + ".proxyHost", host);
    System.setProperty(prefix + ".proxyPort", portString);
    if (nonProxyHosts != null) {
        System.setProperty(prefix + ".nonProxyHosts", nonProxyHosts);
    }
}

From source file:com.emergent.android.weave.client.WeaveBasicObject.java

public URI getUri() throws JSONException {
    if (m_uri == null) {
        try {/*from w  ww.  ja v  a2s. c o m*/
            String baseUriStr = m_queryUri.toASCIIString();
            String queryPart = m_queryUri.getRawQuery();
            if (queryPart != null)
                baseUriStr = baseUriStr.substring(0, baseUriStr.indexOf(queryPart) - 1);
            if (!baseUriStr.endsWith("/"))
                baseUriStr += "/";
            String nodeUriStr = baseUriStr + new URI(null, null, getId(), null).toASCIIString();
            m_uri = new URI(nodeUriStr);
        } catch (URISyntaxException e) {
            throw new JSONException(e.getMessage());
        }
    }
    return m_uri;
}

From source file:com.suarez.cordova.mapsforge.MapsforgePlugin.java

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if ("global-status".equals(action)) {
        this.status();
        callbackContext.success();/*from ww w .ja  v  a2  s. co  m*/
        return true;
    } else if (action.contains("native-")) {
        if ("native-set-center".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.setCenter(args.getDouble(0), args.getDouble(1));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-set-zoom".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.setZoom(Byte.parseByte(args.getString(0)));
                callbackContext.success();
            } catch (NumberFormatException nfe) {
                callbackContext.error("Incorrect argument format. Should be: (byte zoom)");
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-show".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.show();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }
            return true;
        } else if ("native-hide".equals(action)) {
            MapsforgeNative.INSTANCE.hide();
            return true;
        } else if ("native-marker".equals(action)) {
            try {
                Activity context = this.cordova.getActivity();

                int markerId = context.getResources().getIdentifier(args.getString(0), "drawable",
                        context.getPackageName());
                if (markerId == 0) {
                    Log.i(MapsforgePlugin.TAG, "Marker not found...using default marker: marker_green");
                    markerId = context.getResources().getIdentifier("marker_green", "drawable",
                            context.getPackageName());
                }

                int markerKey = MapsforgeNative.INSTANCE.addMarker(markerId, args.getDouble(1),
                        args.getDouble(2));
                callbackContext.success(markerKey);
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-polyline".equals(action)) {

            try {
                JSONArray points = args.getJSONArray(2);

                if (points.length() % 2 != 0)
                    throw new JSONException("Invalid array of coordinates. Length should be multiple of 2");

                int polylineKey = MapsforgeNative.INSTANCE.addPolyline(args.getInt(0), args.getInt(1), points);

                callbackContext.success(polylineKey);
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-delete-layer".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.deleteLayer(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-initialize".equals(action)) {

            try {

                MapsforgeNative.createInstance(this.cordova.getActivity(), args.getString(0), args.getInt(1),
                        args.getInt(2));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-set-max-zoom".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setMaxZoom(Byte.parseByte(args.getString(0)));
                callbackContext.success();
            } catch (NumberFormatException nfe) {
                callbackContext.error("Incorrect argument format. Should be: (byte zoom)");
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-set-min-zoom".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setMinZoom(Byte.parseByte(args.getString(0)));
                callbackContext.success();
            } catch (NumberFormatException nfe) {
                callbackContext.error("Incorrect argument format. Should be: (byte zoom)");
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-show-controls".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setBuiltInZoomControls(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-clickable".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setClickable(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-show-scale".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.showScaleBar(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-destroy-cache".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.destroyCache(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            }

            return true;
        } else if ("native-map-path".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setMapFilePath(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException iae) {
                callbackContext.error(iae.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-cache-name".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setCacheName(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-theme-path".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setRenderThemePath(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-stop".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.onStop();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-start".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.onStart();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-destroy".equals(action)) {
            try {
                MapsforgeNative.INSTANCE.onDestroy();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-online".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setOnline(args.getString(0), args.getString(1), args.getString(2),
                        args.getString(3), args.getInt(4));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("native-offline".equals(action)) {

            try {
                MapsforgeNative.INSTANCE.setOffline(args.getString(0), args.getString(1));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        }
    } else if (action.contains("cache-")) {
        if ("cache-get-tile".equals(action)) {

            try {
                final long x = args.getLong(0);
                final long y = args.getLong(1);
                final byte z = Byte.parseByte(args.getString(2));
                final CallbackContext callbacks = callbackContext;

                cordova.getThreadPool().execute(new Runnable() {
                    public void run() {
                        try {
                            String path = MapsforgeCache.INSTANCE.getTilePath(x, y, z);
                            callbacks.success(path);
                        } catch (IOException e) {
                            callbacks.error(e.getMessage());
                        } catch (Exception e) {
                            callbacks.error(e.getMessage());
                        }
                    }
                });
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (NumberFormatException nfe) {
                callbackContext.error(nfe.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-initialize".equals(action)) {

            try {
                MapsforgeCache.createInstance(cordova.getActivity(), args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (IllegalArgumentException e) {
                callbackContext.error(e.getMessage());
            } catch (IOException e) {
                callbackContext.error(e.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-map-path".equals(action)) {

            try {
                final String mapFile = args.getString(0);
                final CallbackContext callbacks = callbackContext;

                cordova.getThreadPool().execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            MapsforgeCache.INSTANCE.setMapFilePath(mapFile);
                            callbacks.success();
                        } catch (IllegalArgumentException e) {
                            callbacks.error(e.getMessage());
                        } catch (FileNotFoundException e) {
                            callbacks.error(e.getMessage());
                        } catch (Exception e) {
                            callbacks.error(e.getMessage());
                        }
                    }
                });
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-max-size".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setMaxCacheSize(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-max-age".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setMaxCacheAge(args.getLong(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-cleaning-trigger".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCleanCacheTrigger(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-enabled".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCacheEnabled(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-external".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setExternalCache(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-name".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCacheName(args.getString(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-tile-size".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setTileSize(args.getInt(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-clean-destroy".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setCleanOnDestroy(args.getBoolean(0));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-theme-path".equals(action)) {

            try {
                final CallbackContext callbacks = callbackContext;
                final String themePath = args.getString(0);

                cordova.getThreadPool().execute(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            MapsforgeCache.INSTANCE.setRenderTheme(themePath);
                            callbacks.success();
                        } catch (IllegalArgumentException e) {
                            callbacks.error(e.getMessage());
                        } catch (FileNotFoundException e) {
                            callbacks.error(e.getMessage());
                        } catch (Exception e) {
                            callbacks.error(e.getMessage());
                        }
                    }
                });
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-screen-ratio".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setScreenRatio(Float.parseFloat(args.getString(0)));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (NumberFormatException nfe) {
                callbackContext.error(nfe.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-overdraw".equals(action)) {

            try {
                MapsforgeCache.INSTANCE.setOverdrawFactor(Float.parseFloat(args.getString(0)));
                callbackContext.success();
            } catch (JSONException je) {
                callbackContext.error(je.getMessage());
            } catch (NumberFormatException nfe) {
                callbackContext.error(nfe.getMessage());
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        } else if ("cache-destroy".equals(action)) {
            try {
                MapsforgeCache.INSTANCE.onDestroy();
                callbackContext.success();
            } catch (Exception e) {
                callbackContext.error(e.getMessage());
            }

            return true;
        }
    }
    return false; // Returning false results in a "MethodNotFound" error.
}

From source file:com.frublin.androidoauth2.Util.java

/**
 * Parse a server response into a JSON Object. This is a basic
 * implementation using org.json.JSONObject representation. More
 * sophisticated applications may wish to do their own parsing.
 *
 * The parsed JSON is checked for a variety of error fields and
 * a FacebookException is thrown if an error condition is set,
 * populated with the error message and error type or code if
 * available./*w  w  w. ja  v  a 2 s  . c o  m*/
 *
 * @param response - string representation of the response
 * @return the response as a JSON Object
 * @throws JSONException - if the response is not valid JSON
 * @throws FacebookError - if an error condition is set
 */
public static JSONObject parseJson(String response) throws JSONException /*, FacebookError*/ {
    // Edge case: when sending a POST request to /[post_id]/likes
    // the return value is 'true' or 'false'. Unfortunately
    // these values cause the JSONObject constructor to throw
    // an exception.
    if (response.equals("false")) {
        Log.e("Foursquare JSON parse error:", "Request Failed");
        throw new JSONException("Request Failed");
        //throw new FacebookError("request failed");
    }
    if (response.equals("true")) {
        response = "{value : true}";
    }
    JSONObject json = new JSONObject(response);

    // errors set by the server are not consistent
    // they depend on the method and endpoint
    //        if (json.has("error")) {
    //            JSONObject error = json.getJSONObject("error");
    //            throw new FacebookError(
    //                    error.getString("message"), error.getString("type"), 0);
    //        }
    //        if (json.has("error_code") && json.has("error_msg")) {
    //            throw new FacebookError(json.getString("error_msg"), "",
    //                    Integer.parseInt(json.getString("error_code")));
    //        }
    //        if (json.has("error_code")) {
    //            throw new FacebookError("request failed", "",
    //                    Integer.parseInt(json.getString("error_code")));
    //        }
    //        if (json.has("error_msg")) {
    //            throw new FacebookError(json.getString("error_msg"));
    //        }
    //        if (json.has("error_reason")) {
    //            throw new FacebookError(json.getString("error_reason"));
    //        }
    return json;
}

From source file:uk.org.rivernile.android.fetchutils.fetchers.readers.JSONFetcherStreamReader.java

/**
 * Get a {@link JSONObject} version of this data.
 * //from   w  ww.j a v  a  2 s .  c o m
 * @return A {@link JSONObject}, which is the root of the document tree.
 * @throws JSONException If there was an error parsing the JSON text, such as when the data does
 * not represent a {@link JSONObject}.
 */
public JSONObject getJSONObject() throws JSONException {
    final String data = getData();

    if (data == null) {
        throw new JSONException("The data is null.");
    }

    return new JSONObject(data);
}

From source file:uk.org.rivernile.android.fetchutils.fetchers.readers.JSONFetcherStreamReader.java

/**
 * Get a {@link JSONArray} version of this data.
 * /*  ww w.j av a  2s  . co  m*/
 * @return A {@link JSONArray}, which is the root of the document tree.
 * @throws JSONException If there was an error parsing the JSON text, such as when the data does
 * not represent a {@link JSONArray}.
 */
public JSONArray getJSONArray() throws JSONException {
    final String data = getData();

    if (data == null) {
        throw new JSONException("The data is null.");
    }

    return new JSONArray(data);
}

From source file:se.leap.bitmaskclient.Provider.java

public String getName() {
    // Should we pass the locale in, or query the system here?
    String lang = Locale.getDefault().getLanguage();
    String name = "";
    try {/*from   ww  w .  ja v a2 s .co m*/
        if (definition != null)
            name = definition.getJSONObject(API_TERM_NAME).getString(lang);
        else
            throw new JSONException("Provider not defined");
    } catch (JSONException e) {
        if (main_url != null) {
            String host = main_url.getDomain();
            name = host.substring(0, host.indexOf("."));
        }
    }

    return name;
}

From source file:cn.rongcloud.im.server.network.http.JsonHttpResponseHandler.java

@Override
public void onSuccess(final int statusCode, final Header[] headers, final String responseBody) {
    if (statusCode != HttpStatus.SC_NO_CONTENT) {
        new Thread(new Runnable() {
            @Override//from  w ww  .j a  v  a2  s. c o m
            public void run() {
                try {
                    final Object jsonResponse = parseResponse(responseBody);
                    postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            if (jsonResponse instanceof JSONObject) {
                                onSuccess(statusCode, headers, (JSONObject) jsonResponse);
                            } else if (jsonResponse instanceof JSONArray) {
                                onSuccess(statusCode, headers, (JSONArray) jsonResponse);
                            } else if (jsonResponse instanceof String) {
                                onSuccess(statusCode, headers, (String) jsonResponse);
                            } else {
                                onFailure(
                                        new JSONException(
                                                "Unexpected type " + jsonResponse.getClass().getName()),
                                        (JSONObject) null);
                            }

                        }
                    });
                } catch (final JSONException ex) {
                    postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            onFailure(ex, (JSONObject) null);
                        }
                    });
                }
            }
        }).start();
    } else {
        onSuccess(statusCode, headers, new JSONObject());
    }
}

From source file:cn.rongcloud.im.server.network.http.JsonHttpResponseHandler.java

@Override
public void onFailure(final int statusCode, final Header[] headers, final String responseBody,
        final Throwable e) {
    if (responseBody != null) {
        new Thread(new Runnable() {
            @Override//from  w  w w .j a  va 2s .c o  m
            public void run() {
                try {
                    final Object jsonResponse = parseResponse(responseBody);
                    postRunnable(new Runnable() {
                        @SuppressWarnings("deprecation")
                        @Override
                        public void run() {
                            if (jsonResponse instanceof JSONObject) {
                                onFailure(statusCode, headers, e, (JSONObject) jsonResponse);
                            } else if (jsonResponse instanceof JSONArray) {
                                onFailure(statusCode, headers, e, (JSONArray) jsonResponse);
                            } else if (jsonResponse instanceof String) {
                                onFailure(statusCode, headers, e, (String) jsonResponse);
                            } else {
                                onFailure(
                                        new JSONException(
                                                "Unexpected type " + jsonResponse.getClass().getName()),
                                        (JSONObject) null);
                            }
                        }
                    });

                } catch (final JSONException ex) {
                    postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            onFailure(statusCode, headers, ex, (JSONObject) null);
                        }
                    });

                }
            }
        }).start();
    } else {
        Log.v(LOG_TAG, "response body is null, calling onFailure(Throwable, JSONObject)");
        onFailure(statusCode, headers, e, (JSONObject) null);
    }
}

From source file:org.nuxeo.connect.data.AbstractJSONSerializableData.java

public static <T> T loadFromJSON(Class<T> targetClass, JSONObject data) throws JSONException {
    try {//w w  w  .j av  a  2s.com
        return targetClass.cast(doLoadFromJSON(data, targetClass, targetClass.newInstance()));
    } catch (Exception e) {
        throw new JSONException(e);
    }
}