Example usage for org.json JSONObject optString

List of usage examples for org.json JSONObject optString

Introduction

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

Prototype

public String optString(String key) 

Source Link

Document

Get an optional string associated with a key.

Usage

From source file:com.actionlauncher.api.LiveWallpaperInfo.java

/**
 * Deserializes an liveWallpaperInfo object from a {@link JSONObject}.
 *//*  www  .  j av  a2  s. co m*/
public static LiveWallpaperInfo fromJson(JSONObject jsonObject) throws JSONException {
    return new Builder().token(jsonObject.optString(KEY_TOKEN))
            .paletteVibrantRgb(paletteValue(jsonObject, KEY_PALETTE_VIBRANT_RGB))
            .paletteVibrantTitleTextRgb(paletteValue(jsonObject, KEY_PALETTE_VIBRANT_TITLE_TEXT))
            .paletteVibrantBodyTextRgb(paletteValue(jsonObject, KEY_PALETTE_VIBRANT_BODY_TEXT))
            .paletteLightVibrantRgb(paletteValue(jsonObject, KEY_PALETTE_LIGHT_VIBRANT_RGB))
            .paletteLightVibrantTitleTextRgb(paletteValue(jsonObject, KEY_PALETTE_LIGHT_VIBRANT_TITLE_TEXT))
            .paletteLightVibrantBodyTextRgb(paletteValue(jsonObject, KEY_PALETTE_LIGHT_VIBRANT_BODY_TEXT))
            .paletteDarkVibrantRgb(paletteValue(jsonObject, KEY_PALETTE_DARK_VIBRANT_RGB))
            .paletteDarkVibrantTitleTextRgb(paletteValue(jsonObject, KEY_PALETTE_DARK_VIBRANT_TITLE_TEXT))
            .paletteDarkVibrantBodyTextRgb(paletteValue(jsonObject, KEY_PALETTE_DARK_VIBRANT_BODY_TEXT))
            .paletteMutedRgb(paletteValue(jsonObject, KEY_PALETTE_MUTED_RGB))
            .paletteMutedTitleTextRgb(paletteValue(jsonObject, KEY_PALETTE_MUTED_TITLE_TEXT))
            .paletteMutedBodyTextRgb(paletteValue(jsonObject, KEY_PALETTE_MUTED_BODY_TEXT))
            .paletteLightMutedRgb(paletteValue(jsonObject, KEY_PALETTE_LIGHT_MUTED_RGB))
            .paletteLightMutedTitleTextRgb(paletteValue(jsonObject, KEY_PALETTE_LIGHT_MUTED_TITLE_TEXT))
            .paletteLightMutedBodyTextRgb(paletteValue(jsonObject, KEY_PALETTE_LIGHT_MUTED_BODY_TEXT))
            .paletteDarkMutedRgb(paletteValue(jsonObject, KEY_PALETTE_DARK_MUTED_RGB))
            .paletteDarkMutedTitleTextRgb(paletteValue(jsonObject, KEY_PALETTE_DARK_MUTED_TITLE_TEXT))
            .paletteDarkMutedBodyTextRgb(paletteValue(jsonObject, KEY_PALETTE_DARK_MUTED_BODY_TEXT)).build();
}

From source file:com.example.SmartBoard.MQTTHandler.java

public void publishRectangle(JSONObject rectangle) {
    if (!StatusListener.connectFlag)
        return;/*from   w  w w.  ja v  a 2s.  c  om*/

    try {
        client.publish("smartboard/" + Login.roomId + "/objects/" + rectangle.optString("id"),
                rectangle.toString().getBytes(), 0, true);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

From source file:com.example.SmartBoard.MQTTHandler.java

public void publishCircle(JSONObject circle) {
    if (!StatusListener.connectFlag)
        return;/*ww w.ja  va2  s.com*/

    try {
        client.publish("smartboard/" + Login.roomId + "/objects/" + circle.optString("id"),
                circle.toString().getBytes(), 0, true);
    } catch (MqttException e) {
        e.printStackTrace();

    }

}

From source file:com.example.SmartBoard.MQTTHandler.java

public void publishObject(JSONObject object) {
    if (!StatusListener.connectFlag)
        return;//  w  ww  .j  a va2  s  .  c  o  m

    try {
        object.put("clientId", client.getClientId()); //client that modified object position
        client.publish("smartboard/" + Login.roomId + "/objects/" + object.optString("id"),
                object.toString().getBytes(), 0, true);
    } catch (MqttException e) {
        e.printStackTrace();

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

From source file:com.example.SmartBoard.MQTTHandler.java

public void publishLine(JSONObject line) {
    if (!StatusListener.connectFlag)
        return;//  w  w  w.ja v a2s .c om

    try {
        client.publish("smartboard/" + Login.roomId + "/objects/" + line.optString("id"),
                line.toString().getBytes(), 0, true);
    } catch (MqttException e) {
        e.printStackTrace();
    }

}

From source file:com.example.SmartBoard.MQTTHandler.java

public void publishtext(JSONObject textObject) {
    if (!StatusListener.connectFlag)
        return;/*from ww w  .  j a v a2s .co  m*/
    try {
        client.publish("smartboard/" + Login.roomId + "/objects/" + textObject.optString("id"),
                textObject.toString().getBytes(), 0, true);
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

From source file:com.example.SmartBoard.MQTTHandler.java

public void messageArrived(String topic, MqttMessage message) {
    MyActivity drawingActivity = (MyActivity) drawingContext;
    JSONObject recvMessage = null;

    if (message.toString().compareTo("") == 0) {
        //user went offline
        String[] topicStruct = topic.split("/");
        // System.out.println("user to be removed: "+topicStruct[2]);

        if (topicStruct[2].compareTo("users") == 0) {
            usersListHistory.remove(new OnlineStateMessage(null, topicStruct[3]));
            usersAdapter.notifyDataSetChanged();

        } else if (topicStruct[2].compareTo("objects") == 0) {
            drawingActivity.drawer.removeObject(topicStruct[3]);
        }/*  w w w. ja v a  2 s .co m*/
        return;
    }

    try {
        recvMessage = new JSONObject(message.toString());
    } catch (JSONException j) {
        j.printStackTrace();
    }

    if (recvMessage.optString("status").compareTo("online") == 0) {

        OnlineStateMessage newUser = new OnlineStateMessage(recvMessage.optString("selfie"),
                recvMessage.optString("userId"));
        // System.out.println("user added: "+ recvMessage.optString("userId"));
        usersListHistory.add(newUser);
        usersAdapter.notifyDataSetChanged();
        return;

    }

    String clientId = recvMessage.optString("clientId");

    if (clientId.compareTo(client.getClientId()) != 0) {

        switch (type.valueOf(recvMessage.optString("type"))) {

        case Point:
            drawingActivity.drawer.drawPoint((float) recvMessage.optDouble("mX"),
                    (float) recvMessage.optDouble("mY"), recvMessage.optInt("drawActionFlag"),
                    recvMessage.optInt("color"), recvMessage.optString("mode"), recvMessage.optInt("brushSize"),
                    recvMessage.optString("clientId"));
            break;
        case Eraser:
            float eSize = (float) recvMessage.optDouble("size");
            drawingActivity.drawer.updateEraseSize(eSize);
            break;
        case Pencil: //shares topic with Eraser
            float size = (float) recvMessage.optDouble("size");
            drawingActivity.drawer.updateBrushSize(size);
            break;

        case ColorChange:
            drawingActivity.drawer.updateColor(recvMessage.optInt("code"));
            break;

        case ClearScreen:
            drawingActivity.drawer.updateClearScreen();
            break;
        case Chat:
            Vibrator v = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(new long[] { 3, 100 }, -1);

            String[] nameMessage = recvMessage.optString("message").split(":");
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx)
                    .setLargeIcon(stringToBitmap(recvMessage.optString("selfie")))
                    .setSmallIcon(R.drawable.smart2).setContentTitle(nameMessage[0])
                    .setContentText(nameMessage[1]).setTicker("New Message Arrived").setAutoCancel(true)
                    .setNumber(++numMessages);

            NotificationManager mNotificationManager = (NotificationManager) ctx
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(1000, mBuilder.build());

            ChatMessageWithSelfie mChatMessage = new ChatMessageWithSelfie(recvMessage.optBoolean("direction"),
                    recvMessage.optString("message"), recvMessage.optString("selfie"),
                    recvMessage.optString("imageSent"), null);
            sessionHistory.add(mChatMessage);
            Chat.chatAdapter.add(mChatMessage);
            break;

        case Image:
            Vibrator v2 = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
            v2.vibrate(new long[] { 3, 100 }, -1);
            Toast.makeText(ctx, recvMessage.optString("username") + " has sent you a message!",
                    Toast.LENGTH_SHORT).show();
            ChatMessageWithSelfie mChatImageMessage = new ChatMessageWithSelfie(
                    recvMessage.optBoolean("direction"), null, recvMessage.optString("selfie"),
                    recvMessage.optString("image"), null);
            sessionHistory.add(mChatImageMessage);
            Chat.chatAdapter.add(mChatImageMessage);
            break;
        case Rectangle:
            drawingActivity.drawer.onDrawReceivedRectangle(recvMessage);
            break;
        case Circle:
            drawingActivity.drawer.onDrawReceivedCircle(recvMessage);
            break;
        case Line:
            drawingActivity.drawer.onDrawReceivedLine(recvMessage);
            break;
        case Text:
            drawingActivity.drawer.onDrawReceivedText(recvMessage);
            break;
        default:
            //ignore the message
        }
    }
}

From source file:com.github.devnied.emvnfccard.billing.SkuDetails.java

public SkuDetails(final String jsonSkuDetails) throws JSONException {
    mJson = jsonSkuDetails;//from  w w  w .java2s .  c o  m
    JSONObject o = new JSONObject(mJson);
    mSku = o.optString("productId");
    mType = o.optString("type");
    mPrice = o.optString("price");
    mTitle = o.optString("title");
    mDescription = o.optString("description");
}

From source file:com.vk.sdkweb.api.model.VKApiPhotoSize.java

/**
 * Creates dimension from {@code source}. Used in parsing.
 * If size is not specified copies calculates them based on internal algorithms.
 * @param source object in format, returned VK API, which is generated from the dimension
 * @param originalWidth original image width in pixels
 * @param originalHeight original image height in pixels
 *//*w  w w. j a  va2s.c  om*/
public static VKApiPhotoSize parse(JSONObject source, int originalWidth, int originalHeight) {
    VKApiPhotoSize result = new VKApiPhotoSize();
    result.src = source.optString("src");
    result.width = source.optInt("width");
    result.height = source.optInt("height");
    String type = source.optString("type");
    if (!TextUtils.isEmpty(type)) {
        result.type = type.charAt(0);
    }
    // ? ,   ? ? ?  .
    // ? , ??, width  height  ???   ? .
    // ??    .
    if (result.width == 0 || result.height == 0) {
        fillDimensions(result, originalWidth, originalHeight);
    }
    return result;
}

From source file:ru.orangesoftware.financisto2.rates.OpenExchangeRatesDownloader.java

private String error(JSONObject json) {
    String status = json.optString("status");
    String message = json.optString("message");
    String description = json.optString("description");
    return status + " (" + message + "): " + description;
}