Example usage for org.json JSONException printStackTrace

List of usage examples for org.json JSONException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

/**
 * Calls the Web callback with an object containing response fields
 * @param plugin: the name of the plugin.
 * @param data: the object containing response fields
 * @param callbackID: the Web callback.//from w ww. j  a  va  2  s  .  co  m
 */
public void sendPlugin(final String plugin, final JSONObject data, final String callbackID) {
    if (plugin != null && plugin.length() > 0) {
        try {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put(Cobalt.kJSType, Cobalt.JSTypePlugin);
            jsonObj.put(Cobalt.kJSPluginName, plugin);
            jsonObj.put(Cobalt.kJSData, data);
            jsonObj.put(Cobalt.kJSCallback, callbackID);
            executeScriptInWebView(jsonObj);
        } catch (JSONException exception) {
            if (Cobalt.DEBUG)
                Log.e(Cobalt.TAG, TAG + " - sendPlugin: JSONException");
            exception.printStackTrace();
        }
    } else if (Cobalt.DEBUG)
        Log.e(Cobalt.TAG, TAG + " - sendPlugin: plugin is null or empty!");
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

/**
 * This method is called when the JavaScript sends a message to the native side.
 * This method should be overridden in subclasses.
 * @param message : the JSON-message sent by JavaScript.
 * @return true if the message was handled by the native, false otherwise
 * @details some basic operations are already implemented : navigation, logs, toasts, native alerts, web alerts
 * @details this method may be called from a secondary thread.
 *//*from   w w w  .  j ava2  s. c  o m*/
// This method must be public !!!
@JavascriptInterface
public boolean onCobaltMessage(String message) {
    try {
        final JSONObject jsonObj = new JSONObject(message);

        // TYPE
        if (jsonObj.has(Cobalt.kJSType)) {
            String type = jsonObj.getString(Cobalt.kJSType);

            //CALLBACK
            if (type.equals(Cobalt.JSTypeCallBack)) {
                String callbackID = jsonObj.getString(Cobalt.kJSCallback);
                JSONObject data = jsonObj.optJSONObject(Cobalt.kJSData);

                return handleCallback(callbackID, data);
            }

            // COBALT IS READY
            else if (type.equals(Cobalt.JSTypeCobaltIsReady)) {
                String versionWeb = jsonObj.optString(Cobalt.kJSVersion, null);
                String versionNative = getResources().getString(R.string.version_name);
                if (versionWeb != null && !versionWeb.equals(versionNative))
                    Log.e(TAG, "Warning : Cobalt version mismatch : Android Cobalt version is " + versionNative
                            + " but Web Cobalt version is " + versionWeb + ". You should fix this. ");
                onCobaltIsReady();
                return true;
            }

            // EVENT
            else if (type.equals(Cobalt.JSTypeEvent)) {
                String event = jsonObj.getString(Cobalt.kJSEvent);
                JSONObject data = jsonObj.optJSONObject(Cobalt.kJSData);
                String callback = jsonObj.optString(Cobalt.kJSCallback, null);

                return handleEvent(event, data, callback);
            }

            // INTENT
            else if (type.equals(Cobalt.JSTypeIntent)) {
                String action = jsonObj.getString(Cobalt.kJSAction);

                // OPEN EXTERNAL URL
                if (action.equals(Cobalt.JSActionIntentOpenExternalUrl)) {
                    JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);
                    String url = data.getString(Cobalt.kJSUrl);
                    openExternalUrl(url);

                    return true;
                }

                // UNHANDLED INTENT
                else {
                    onUnhandledMessage(jsonObj);
                }
            }

            // LOG
            else if (type.equals(Cobalt.JSTypeLog)) {
                String text = jsonObj.getString(Cobalt.kJSValue);
                Log.d(Cobalt.TAG, "JS LOG: " + text);
                return true;
            }

            // NAVIGATION
            else if (type.equals(Cobalt.JSTypeNavigation)) {
                String action = jsonObj.getString(Cobalt.kJSAction);

                // PUSH
                if (action.equals(Cobalt.JSActionNavigationPush)) {
                    JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);
                    String page = data.getString(Cobalt.kJSPage);
                    String controller = data.optString(Cobalt.kJSController, null);
                    push(controller, page);
                    return true;
                }

                // POP
                else if (action.equals(Cobalt.JSActionNavigationPop)) {
                    JSONObject data = jsonObj.optJSONObject(Cobalt.kJSData);
                    if (data != null) {
                        String page = data.getString(Cobalt.kJSPage);
                        String controller = data.optString(Cobalt.kJSController, null);
                        pop(controller, page);
                    } else
                        pop();
                    return true;
                }

                // MODAL
                else if (action.equals(Cobalt.JSActionNavigationModal)) {
                    JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);
                    String page = data.getString(Cobalt.kJSPage);
                    String controller = data.optString(Cobalt.kJSController, null);
                    String callbackId = jsonObj.optString(Cobalt.kJSCallback, null);
                    presentModal(controller, page, callbackId);
                    return true;
                }

                // DISMISS
                else if (action.equals(Cobalt.JSActionNavigationDismiss)) {
                    // TODO: not present in iOS
                    JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);
                    String controller = data.getString(Cobalt.kJSController);
                    String page = data.getString(Cobalt.kJSPage);
                    dismissModal(controller, page);
                    return true;
                }

                //REPLACE
                else if (action.equals(Cobalt.JSActionNavigationReplace)) {
                    JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);
                    String controller = data.getString(Cobalt.kJSController);
                    String page = data.getString(Cobalt.kJSPage);
                    boolean animated = data.optBoolean(Cobalt.kJSAnimated);
                    replace(controller, page, animated);
                    return true;
                }

                // UNHANDLED NAVIGATION
                else {
                    onUnhandledMessage(jsonObj);
                }
            }

            // PLUGIN
            else if (type.equals(Cobalt.JSTypePlugin)) {
                mPluginManager.onMessage(mContext, this, jsonObj);
            }

            // UI
            else if (type.equals(Cobalt.JSTypeUI)) {
                String control = jsonObj.getString(Cobalt.kJSUIControl);
                JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);
                String callback = jsonObj.optString(Cobalt.kJSCallback, null);

                return handleUi(control, data, callback);
            }

            // WEB LAYER
            else if (type.equals(Cobalt.JSTypeWebLayer)) {
                String action = jsonObj.getString(Cobalt.kJSAction);

                // SHOW
                if (action.equals(Cobalt.JSActionWebLayerShow)) {
                    final JSONObject data = jsonObj.getJSONObject(Cobalt.kJSData);

                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            showWebLayer(data);
                        }
                    });

                    return true;
                }

                // UNHANDLED WEB LAYER
                else {
                    onUnhandledMessage(jsonObj);
                }
            }

            // UNHANDLED TYPE
            else {
                onUnhandledMessage(jsonObj);
            }
        }

        // UNHANDLED MESSAGE
        else {
            onUnhandledMessage(jsonObj);
        }
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - onCobaltMessage: JSONException");
        exception.printStackTrace();
    } catch (NullPointerException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - onCobaltMessage: NullPointerException");
        exception.printStackTrace();
    }

    return false;
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

private boolean handleCallback(String callback, JSONObject data) {
    switch (callback) {
    case Cobalt.JSCallbackOnBackButtonPressed:
        try {/* w  w  w.j ava  2 s  .  c o m*/
            onBackPressed(data.getBoolean(Cobalt.kJSValue));
            return true;
        } catch (JSONException exception) {
            if (Cobalt.DEBUG)
                Log.e(Cobalt.TAG, TAG + " - handleCallback: JSONException");
            exception.printStackTrace();
            return false;
        }
    case Cobalt.JSCallbackPullToRefreshDidRefresh:
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                onPullToRefreshDidRefresh();
            }
        });
        return true;
    case Cobalt.JSCallbackInfiniteScrollDidRefresh:
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                onInfiniteScrollDidRefresh();
            }
        });
        return true;
    default:
        return onUnhandledCallback(callback, data);
    }
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

private boolean handleUi(String control, JSONObject data, String callback) {
    try {//from   ww w  . jav  a2  s.c  o m
        // PICKER
        switch (control) {
        case Cobalt.JSControlPicker:
            String type = data.getString(Cobalt.kJSType);

            // DATE
            if (type.equals(Cobalt.JSPickerDate)) {
                JSONObject date = data.optJSONObject(Cobalt.kJSDate);

                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);

                if (date != null && date.has(Cobalt.kJSYear) && date.has(Cobalt.kJSMonth)
                        && date.has(Cobalt.kJSDay)) {
                    year = date.getInt(Cobalt.kJSYear);
                    month = date.getInt(Cobalt.kJSMonth) - 1;
                    day = date.getInt(Cobalt.kJSDay);
                }

                JSONObject texts = data.optJSONObject(Cobalt.kJSTexts);
                String title = texts.optString(Cobalt.kJSTitle, null);
                //String delete = texts.optString(Cobalt.kJSDelete, null);
                String clear = texts.optString(Cobalt.kJSClear, null);
                String cancel = texts.optString(Cobalt.kJSCancel, null);
                String validate = texts.optString(Cobalt.kJSValidate, null);

                showDatePickerDialog(year, month, day, title, clear, cancel, validate, callback);

                return true;
            }

            break;
        case Cobalt.JSControlAlert:
            showAlertDialog(data, callback);
            return true;
        case Cobalt.JSControlToast:
            String message = data.getString(Cobalt.kJSMessage);
            Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
            return true;
        default:
            break;
        }
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - handleUi: JSONException");
        exception.printStackTrace();
    }

    // UNHANDLED UI
    try {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put(Cobalt.kJSType, Cobalt.JSTypeUI);
        jsonObj.put(Cobalt.kJSUIControl, control);
        jsonObj.put(Cobalt.kJSData, data);
        jsonObj.put(Cobalt.kJSCallback, callback);
        onUnhandledMessage(jsonObj);
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - handleUi: JSONException");
        exception.printStackTrace();
    }

    return false;
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

private void presentModal(String controller, String page, String callBackID) {
    Intent intent = Cobalt.getInstance(mContext).getIntentForController(controller, page);

    if (intent != null) {
        intent.putExtra(Cobalt.kPushAsModal, true);

        mContext.startActivity(intent);/*from  www.  j a  v a  2 s. co m*/

        // Sends callback to store current activity & HTML page for dismiss
        try {
            JSONObject data = new JSONObject();
            data.put(Cobalt.kJSPage, getPage());
            data.put(Cobalt.kJSController, mContext.getClass().getName());
            sendCallback(callBackID, data);
        } catch (JSONException exception) {
            if (Cobalt.DEBUG)
                Log.e(Cobalt.TAG, TAG + " - presentModal: JSONException");
            exception.printStackTrace();
        }
    } else if (Cobalt.DEBUG)
        Log.e(Cobalt.TAG, TAG + " - presentModal: Unable to present modal " + controller + " controller");
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

/***********************************************************************************************************************************
 * WEB LAYER//from  w  w  w  .jav  a  2 s.  co  m
 **********************************************************************************************************************************/
private void showWebLayer(JSONObject data) {
    try {
        String page = data.getString(Cobalt.kJSPage);
        double fadeDuration = data.optDouble(Cobalt.kJSWebLayerFadeDuration, 0.3);

        Bundle bundle = new Bundle();
        bundle.putString(Cobalt.kPage, page);

        CobaltWebLayerFragment webLayerFragment = getWebLayerFragment();

        if (webLayerFragment != null) {
            webLayerFragment.setArguments(bundle);

            FragmentTransaction fragmentTransition = ((FragmentActivity) mContext).getSupportFragmentManager()
                    .beginTransaction();

            if (fadeDuration > 0) {
                fragmentTransition.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                        android.R.anim.fade_in, android.R.anim.fade_out);
            } else {
                fragmentTransition.setTransition(FragmentTransaction.TRANSIT_NONE);
            }

            if (CobaltActivity.class.isAssignableFrom(mContext.getClass())) {
                // Dismiss current Web layer if one is already shown
                CobaltActivity activity = (CobaltActivity) mContext;
                Fragment currentFragment = activity.getSupportFragmentManager()
                        .findFragmentById(activity.getFragmentContainerId());
                if (currentFragment != null
                        && CobaltWebLayerFragment.class.isAssignableFrom(currentFragment.getClass())) {
                    ((CobaltWebLayerFragment) currentFragment).dismissWebLayer(null);
                }

                // Shows Web layer
                if (activity.findViewById(activity.getFragmentContainerId()) != null) {
                    fragmentTransition.add(activity.getFragmentContainerId(), webLayerFragment);
                    fragmentTransition.commit();
                } else if (Cobalt.DEBUG)
                    Log.e(Cobalt.TAG, TAG + " - showWebLayer: fragment container not found");
            }
        } else if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - showWebLayer: getWebLayerFragment returned null!");
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - showWebLayer: JSONException");
        exception.printStackTrace();
    }
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

/**
 * Called from the corresponding {@link CobaltWebLayerFragment} when dismissed.
 * This method may be overridden in subclasses.
 *///from  w w  w  . j av a 2 s  . co  m
public void onWebLayerDismiss(final String page, final JSONObject data) {
    mHandler.post(new Runnable() {

        @Override
        public void run() {
            try {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put(Cobalt.kJSPage, page);
                jsonObj.put(Cobalt.kJSData, data);

                sendEvent(Cobalt.JSEventWebLayerOnDismiss, jsonObj, null);
            } catch (JSONException exception) {
                if (Cobalt.DEBUG)
                    Log.e(Cobalt.TAG, TAG + " - onWebLayerDismiss: JSONException");
                exception.printStackTrace();
            }
        }
    });
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

/******************************************************************************************************************
 * ALERT DIALOG// www.  j a  v a2 s .  c  o  m
 *****************************************************************************************************************/

private void showAlertDialog(JSONObject data, final String callback) {
    try {
        String title = data.optString(Cobalt.kJSAlertTitle);
        String message = data.optString(Cobalt.kJSMessage);
        boolean cancelable = data.optBoolean(Cobalt.kJSAlertCancelable, false);
        JSONArray buttons = data.has(Cobalt.kJSAlertButtons) ? data.getJSONArray(Cobalt.kJSAlertButtons)
                : new JSONArray();

        AlertDialog alertDialog = new AlertDialog.Builder(mContext).setTitle(title).setMessage(message)
                .create();
        alertDialog.setCancelable(cancelable);

        if (buttons.length() == 0) {
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (callback != null) {
                        try {
                            JSONObject data = new JSONObject();
                            data.put(Cobalt.kJSAlertButtonIndex, 0);
                            sendCallback(callback, data);
                        } catch (JSONException exception) {
                            if (Cobalt.DEBUG)
                                Log.e(Cobalt.TAG, TAG + ".AlertDialog - onClick: JSONException");
                            exception.printStackTrace();
                        }
                    }
                }
            });
        } else {
            int buttonsLength = Math.min(buttons.length(), 3);
            for (int i = 0; i < buttonsLength; i++) {
                int buttonId;

                switch (i) {
                case 0:
                default:
                    buttonId = DialogInterface.BUTTON_NEGATIVE;
                    break;
                case 1:
                    buttonId = DialogInterface.BUTTON_NEUTRAL;
                    break;
                case 2:
                    buttonId = DialogInterface.BUTTON_POSITIVE;
                    break;
                }

                alertDialog.setButton(buttonId, buttons.getString(i), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (callback != null) {
                            int buttonIndex;
                            switch (which) {
                            case DialogInterface.BUTTON_NEGATIVE:
                            default:
                                buttonIndex = 0;
                                break;
                            case DialogInterface.BUTTON_NEUTRAL:
                                buttonIndex = 1;
                                break;
                            case DialogInterface.BUTTON_POSITIVE:
                                buttonIndex = 2;
                                break;
                            }

                            try {
                                JSONObject data = new JSONObject();
                                data.put(Cobalt.kJSAlertButtonIndex, buttonIndex);
                                sendCallback(callback, data);
                            } catch (JSONException exception) {
                                if (Cobalt.DEBUG)
                                    Log.e(Cobalt.TAG, TAG + ".AlertDialog - onClick: JSONException");
                                exception.printStackTrace();
                            }
                        }
                    }
                });
            }
        }

        alertDialog.show();
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - showAlertDialog: JSONException");
        exception.printStackTrace();
    }
}

From source file:fr.cobaltians.cobalt.fragments.CobaltFragment.java

protected void sendDate(int year, int month, int day, String callbackID) {
    try {/*from w  w w.j  ava 2s  .  co  m*/
        if (year != -1 && month != -1 && day != -1) {
            JSONObject date = new JSONObject();
            date.put(Cobalt.kJSYear, year);
            date.put(Cobalt.kJSMonth, ++month);
            date.put(Cobalt.kJSDay, day);

            sendCallback(callbackID, date);
        } else {
            sendCallback(callbackID, null);
        }
    } catch (JSONException e) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - sendDate: JSONException");
        e.printStackTrace();
    }
}

From source file:com.sogistudio.online.extendables.SpikaAsync.java

@Override
protected Result doInBackground(Params... params) {
    Result result = null;/*w w  w .  java 2 s.c om*/
    try {
        result = (Result) backgroundWork(params);
    } catch (JSONException e) {
        exception = e;
        e.printStackTrace();
    } catch (IOException e) {
        exception = e;
        e.printStackTrace();
    } catch (SpikaException e) {
        exception = e;
        e.printStackTrace();
    } catch (NullPointerException e) {
        exception = e;
        e.printStackTrace();
    } catch (SpikaForbiddenException e) {
        exception = e;
        e.printStackTrace();
    }
    return result;
}