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.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java

@SuppressWarnings("deprecation")
public Notification createNotification(Bundle settings, JSONObject obj) {

    // notifications[i] = api.normalizeAPI({
    // 'string': {
    // mandatory: ['title', 'body'],
    // defaults: {'icon':'icon'}
    // },//from   w w w . ja v a  2  s.  c o  m
    //
    // 'number': {
    // mandatory: ['id', 'timeMillisSince1970'],
    // // the number of ms after which we start prioritising more recent
    // things above you.
    // defaults: {'epsilon': 1000 * 60 * 24 * 365}
    // },
    //
    // 'boolean': {
    // defaults: {
    // 'vibrate': false,
    // 'sound': false
    // }
    // }

    int icon = settings.getInt("notification_icon", -1);
    if (icon == -1) {
        Log.e(C.TAG, "Need a notification_icon resource in the meta-data of LocalNotificationsAlarmReceiver");
        return null;
    }

    Notification n = new Notification();
    n.icon = icon;
    n.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    long alarmTime = obj.optLong("timeMillisSince1970");
    long displayTime = obj.optLong("displayTimestamp", alarmTime);
    n.when = displayTime;
    n.tickerText = obj.optString("body");
    n.setLatestEventInfo(mContext, obj.optString("title"), obj.optString("body"), null);

    if (obj.optBoolean("vibrate")) {
        n.defaults |= Notification.DEFAULT_VIBRATE;
    }
    if (obj.optBoolean("sound")) {
        n.defaults |= Notification.DEFAULT_SOUND;
    }

    String uriString = settings.getString("content_uri_prefix");
    if (uriString == null) {
        Log.e(C.TAG, "Need a content_uri_prefix in the meta-data of LocalNotificationsAlarmReceiver");
        return null;
    }

    if (uriString.contains("%d")) {
        uriString = String.format(uriString, obj.optInt("id"));
    }
    Uri uri = Uri.parse(uriString);

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setData(uri);
    n.contentIntent = PendingIntent.getActivity(mContext, 23, intent, PendingIntent.FLAG_ONE_SHOT);
    return n;
}

From source file:org.catnut.service.ComposeTweetService.java

/**
 * ?/*ww w  .j av  a  2s.  c  om*/
 *
 * @param response
 * @throws JSONException
 */
private void parseNetworkResponse(HttpClient.UploadResponse response, Draft draft) throws Exception {
    if (response.statusCode == 200) {
        // ?
        JSONObject json = new JSONObject(response.response);
        mSendWeiboProcessor.asyncProcess(this, json);
    } else {
        JSONObject error = new JSONObject(response.response);
        fallback(draft, error.optString(WeiboAPIError.ERROR));
    }
}

From source file:com.polyvi.xface.extension.messaging.XMessagingExt.java

/**
 * ?//from w w w  . j  a  v  a2 s  . co m
 *
 * @param comparisonMsg
 *            ??
 * @param folderType
 *            
 * @param startIndex
 *            
 * @param endIndex
 *            ?
 * @return ?
 */
private JSONArray findMessages(JSONObject comparisonMsg, String folderType, int startIndex, int endIndex)
        throws JSONException {
    // TODO:???Email?
    if (null == folderType) {// folderTypenull?
        folderType = FOLDERTYPE_DRAFT;
    }

    ArrayList<String> projections = new ArrayList<String>();
    projections.add("_id");
    projections.add("subject");
    projections.add("address");
    projections.add("body");
    ArrayList<String> projectionsValue = new ArrayList<String>();
    projectionsValue.add(comparisonMsg.optString("messageId"));
    projectionsValue.add(comparisonMsg.optString("subject"));
    projectionsValue.add(comparisonMsg.optString("destinationAddresses"));
    projectionsValue.add(comparisonMsg.optString("body"));

    StringBuilder selection = XUtils.constructSelectionStatement(projections, projectionsValue);

    int isRead = comparisonMsg.getInt("isRead");
    if (-1 != isRead) {
        if (null == selection) {
            selection = new StringBuilder();
        } else {
            selection.append(" AND ");
        }
        selection.append("read");
        selection.append("=");
        selection.append(isRead);
    }
    String selectionStr = null;
    if (null != selection) {
        selectionStr = selection.toString();
    }

    folderType = folderType.toLowerCase();
    Uri findUri = Uri.withAppendedPath(mSMSContentUri, folderType);
    JSONArray messages = new JSONArray();
    try {
        ContentResolver resolver = mContext.getContentResolver();
        Cursor cursor = resolver.query(findUri, null, selectionStr, null, null);
        if (null == cursor) {
            return messages;
        }
        int count = endIndex - startIndex + 1;
        if (cursor.moveToPosition(startIndex)) {
            do {
                JSONObject message = getMessageFromCursor(cursor);
                messages.put(message);
                count--;
            } while (cursor.moveToNext() && count > 0);
        }
        cursor.close();
    } catch (SQLiteException ex) {
        ex.printStackTrace();
    }
    return messages;
}

From source file:com.abeo.tia.noordin.ProcessCasePurchaser.java

private void setallvalues(String arg2) {

    JSONArray jObj = null;/*from  w w w . jav  a  2  s . c  om*/
    try {
        jObj = new JSONArray(arg2.toString());

        for (int i = 0; i < jObj.length(); i++) {
            JSONObject jsonobject = jObj.getJSONObject(i);
            file_open_date.setText(jsonobject.getString("FileOpenDate"));
            case_file_no.setText(jsonobject.getString("CaseFileNo"));
            case_type.setText(jsonobject.getString("CaseType"));
            Scase_status = jsonobject.getString("CaseStatus");

            JSONObject obj1 = jsonobject.getJSONObject("Purchaser");

            spa_date.setText(obj1.getString("PurSPADate"));
            entry_private_caveat.setText(obj1.getString("PurEntryOfPrivateCaveat"));
            withdrawal_private_caveat.setText(obj1.getString("PurWithOfPrivateCaveat"));
            name1.setText(obj1.getString("PurFirstName"));
            brn_no1.setText(obj1.getString("PurFirstID"));
            tax_no1.setText(obj1.getString("PurFirstTaxNo"));
            contact_no1.setText(obj1.getString("PurFirstContactNo"));
            name2.setText(obj1.getString("PurSecName"));
            brn_no2.setText(obj1.getString("PurSecID"));
            tax_no2.setText(obj1.getString("PurSecTaxNo"));
            contact_no2.setText(obj1.getString("PurSecContactNo"));
            name3.setText(obj1.getString("PurThirdName"));
            brn_no3.setText(obj1.getString("PurThirdID"));
            tax_no3.setText(obj1.getString("PurThirdTaxNo"));
            contact_no3.setText(obj1.getString("PurThirdContactNo"));
            name4.setText(obj1.getString("PurFourthName"));
            brn_no4.setText(obj1.getString("PurFourthID"));
            tax_no4.setText(obj1.optString("PurFourthTaxNo"));
            contact_no4.setText(obj1.getString("PurFourthContactNo"));

            if (!jsonobject.getString("KIV").equals(""))
                Skiv = jsonobject.getString("KIV");
            if (!obj1.getString("PurFirstType").equals(""))
                Stype1 = obj1.getString("PurFirstType");
            if (!obj1.getString("PurSecType").equals(""))
                Stype2 = obj1.getString("PurSecType");
            if (!obj1.getString("PurThirdType").equals(""))
                Stype3 = obj1.getString("PurThirdType");
            if (!obj1.getString("PurFourthType").equals(""))
                Stype4 = obj1.getString("PurFourthType");

            if (obj1.getString("PurRepresentedByFirm").equals("N")) {
                rep_firm.setChecked(false);
                //rep_firm.setEnabled(false);
            } else {
                rep_firm.setChecked(true);
                //rep_firm.setEnabled(false);
            }
            if (obj1.getString("PurlawyerRepresented").equals("N")) {
                lawer_rep.setChecked(false);
                //lawer_rep.setEnabled(false);
            } else {
                lawer_rep.setChecked(true);
                //lawer_rep.setEnabled(false);
            }
        }
        System.out.println(jObj);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    try {
        dropdownstatus();
        dropdownKIV();
        dropdowntype1();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

@Override
protected void onUnhandledMessage(final JSONObject message) {
    try {/* w  ww  .  j  a  va2 s.  co  m*/
        String type = message.optString(Cobalt.kJSType);

        if (type.equals(Cobalt.JSTypeWebLayer)) {
            String action = message.getString(Cobalt.kJSAction);

            if (action.equals(Cobalt.JSActionWebLayerDismiss)) {
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        dismissWebLayer(message);
                    }
                });
            }
        }
    } catch (JSONException exception) {
        if (Cobalt.DEBUG)
            Log.e(Cobalt.TAG, TAG + " - onUnhandledMessage: JSONException");
        exception.printStackTrace();
    }
}

From source file:com.fuse.billing.android.Purchase.java

public Purchase(String jsonPurchaseInfo) throws JSONException {
    mOriginalJson = jsonPurchaseInfo;//from   ww w  . j  ava2s.c om
    JSONObject o = new JSONObject(mOriginalJson);
    mOrderId = o.optString("orderId");
    mPackageName = o.optString("packageName");
    mSku = o.optString("productId");
    mPurchaseTime = o.optLong("purchaseTime");
    mPurchaseState = o.optInt("purchaseState");
    mDeveloperPayload = o.optString("developerPayload");
    mToken = o.optString("token", o.optString("purchaseToken"));
    mIsAutoRenewing = o.optBoolean("autoRenewing");
    mSignature = o.optString("signature");
    mItemType = o.optString("itemType");
}

From source file:com.google.android.gcm.demo.app.GcmBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    ctx = context;//from  w w w  . java 2 s . co m
    String messageType = gcm.getMessageType(intent);
    if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
        sendNotification("Send error: " + intent.getExtras().toString());
    } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
        sendNotification("Deleted messages on server: " + intent.getExtras().toString());
    } else {
        sendNotification("Received: " + intent.getExtras().toString());
        // "urn":"soundcloud:sounds:125","finished_at":"0001-01-01T00:00:00Z","last_played_at":"0001-01-01T00:00:00Z","progress":0}

        LLQueue queue = LLQueue.get();
        if (queue == null)
            return;

        //queue.loadListenLaterQueue();

        if (intent.hasExtra("set")) {
            String set = intent.getStringExtra("set");
            try {
                JSONObject obj = new JSONObject(set);
                String urn = obj.optString("urn");
                Log.d(TAG, "GCM set with urn:" + urn);

                if (!TextUtils.isEmpty(urn)) {
                    queue.addUrn(urn);
                }
            } catch (JSONException e) {
                Log.w(TAG, e);
            }

        } else if (intent.hasExtra("delete")) {
            String delete = intent.getStringExtra("delete");
            try {
                JSONObject obj = new JSONObject(delete);
                String urn = obj.optString("urn");
                Log.d(TAG, "GCM set with urn:" + urn);

                if (!TextUtils.isEmpty(urn)) {
                    queue.removeUrn(urn);
                }

            } catch (JSONException e) {
                Log.w(TAG, e);
            }
        } else if (intent.hasExtra("play")) {

            String play = intent.getStringExtra("play");
            try {
                JSONObject obj = new JSONObject(play);
                String urn = obj.optString("urn");
                String toggleAt = obj.optString("toggle_at");
                long progress = obj.optInt("progress", 0);

                Log.d(TAG, "GCM play with urn:" + urn + " ,togglet_at:" + toggleAt);

                Intent playIntent = new Intent();

                queue.playUrn(urn, progress);

                if (!TextUtils.isEmpty(toggleAt)) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");//spec for RFC3339 (with fractional seconds)
                    try {
                        Date date = format.parse(toggleAt);

                        Log.d(TAG, "parsed date:" + date);

                        //                            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                        //                            alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(), PendingIntent.getActivity(context, 0, playIntent, 0));
                    } catch (ParseException e) {
                        Log.w(TAG, e);
                    }
                } else {

                }

            } catch (JSONException e) {
                Log.w(TAG, e);
            }

        }
    }
    setResultCode(Activity.RESULT_OK);
}

From source file:com.kreggysoft.Billing.util.SkuDetails.java

public SkuDetails(String jsonSkuDetails) throws JSONException {
    mJson = jsonSkuDetails;//from w ww .j  a  va  2 s.  co 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:edu.stanford.mobisocial.dungbeetle.feed.objects.MusicObj.java

private String asText(JSONObject obj) {
    StringBuilder status = new StringBuilder();
    String a = obj.optString(ARTIST);
    String b = obj.optString(TRACK);
    if (b == null || b.length() == 0) {
        b = obj.optString(ALBUM);/*  w ww.j a  v  a  2s  .  c  om*/
    }
    status.append(a).append(" - ").append(b);
    return status.toString();
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.MusicObj.java

@Override
public void activate(Context context, SignedObj obj) {
    JSONObject content = obj.getJson();
    if (content.has(URL)) {
        Intent view = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse(content.optString(URL));
        String type = "audio/x-mpegurl";
        if (content.has(MIME_TYPE)) {
            type = content.optString(MIME_TYPE);
        }/*  w  w w  .  j av  a  2 s.c  o m*/
        view.setDataAndType(uri, type);
        if (!(context instanceof Activity)) {
            view.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(view);
    }
}