Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

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

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

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

/**
 * Calls the Web callback with an object containing response fields
 * @param callbackId: the Web callback.//w  w w  .j a v a 2 s . c o  m
 * @param data: the object containing response fields
 */
public void sendCallback(final String callbackId, final JSONObject data) {
    if (callbackId != null && callbackId.length() > 0) {
        try {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put(Cobalt.kJSType, Cobalt.JSTypeCallBack);
            jsonObj.put(Cobalt.kJSCallback, callbackId);
            jsonObj.put(Cobalt.kJSData, data);
            executeScriptInWebView(jsonObj);
        } catch (JSONException exception) {
            if (Cobalt.DEBUG)
                Log.e(Cobalt.TAG, TAG + " - sendCallback: JSONException");
            exception.printStackTrace();
        }
    } else if (Cobalt.DEBUG)
        Log.e(Cobalt.TAG, TAG + " - sendCallback: callbackId is null or empty!");
}

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

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

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 .  jav  a  2 s.c  o  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

private boolean handleUi(String control, JSONObject data, String callback) {
    try {/*  w  w  w.ja  va2 s. c om*/
        // 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);// ww  w. j a  va2 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

/**
 * Called from the corresponding {@link CobaltWebLayerFragment} when dismissed.
 * This method may be overridden in subclasses.
 *///from   w  w  w  .j  a  va 2s . 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//from   www  . ja  v  a2s .com
 *****************************************************************************************************************/

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 {//  www .j a  va 2 s .  c om
        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:org.account.LoginServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w w  w  .ja  v  a2s.c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    JSONObject jsonResponse = new JSONObject();
    PrintWriter out = response.getWriter();

    String email = request.getParameter("email");
    String password = request.getParameter("password");

    if (Login.checkUser(email, password)) {
        int retid = Login.findUserId(email, password);
        jsonResponse.put("id", retid);
        jsonResponse.put("username", Login.findUsernamebyId(retid));
        out.write(jsonResponse.toString());
        out.flush();
    } else {
        jsonResponse.put("id", "Login failed");
        jsonResponse.put("username", "nil");
        out.write(jsonResponse.toString());
        out.flush();
    }
}

From source file:io.ingame.squarecamera.CameraLauncher.java

private void setOutputUri(Intent intent, Uri uri) {
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);

    JSONObject response;
    try {//from  w  ww  .  jav  a  2  s  .  co m
        response = new JSONObject();
        response.put("uri", uri.toString());
        response.put("pending", true);
    } catch (JSONException e) {
        Log.e(LOG_TAG, "On pending result creation" + e);
        return;
    }

    PluginResult r = new PluginResult(PluginResult.Status.OK, response);
    r.setKeepCallback(true);
    callbackContext.sendPluginResult(r);
}