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:org.brickred.socialauth.provider.InstagramImpl.java

@Override
public List<Contact> getContactList() throws Exception {
    LOG.info("Fetching contacts from " + CONTACTS_URL);
    Response serviceResponse;/* w w  w  .  j  a  v  a2s  .  co  m*/
    try {
        serviceResponse = authenticationStrategy.executeFeed(CONTACTS_URL);
    } catch (Exception e) {
        throw new SocialAuthException(
                "Error : " + e.getMessage() + " - while getting contacts from " + CONTACTS_URL, e);
    }

    if (serviceResponse.getStatus() != 200) {
        throw new SocialAuthException("Error while getting contacts from " + CONTACTS_URL + "Status : "
                + serviceResponse.getStatus());
    }

    String respStr = serviceResponse.getResponseBodyAsString(Constants.ENCODING);
    LOG.debug("Contacts JSON string :: " + respStr);
    List<Contact> plist = new ArrayList<Contact>();

    JSONObject resp = new JSONObject(respStr);
    JSONArray data = resp.optJSONArray("data");
    if (data != null) {
        for (int i = 0; i < data.length(); i++) {
            JSONObject obj = data.getJSONObject(i);
            Contact p = new Contact();
            String id = obj.optString("id");
            p.setId(id);
            String full_name = obj.optString("full_name");
            p.setDisplayName(full_name);
            if (full_name != null) {
                String[] names = full_name.split(" ");
                if (names.length > 1) {
                    p.setFirstName(names[0]);
                    p.setLastName(names[1]);
                } else {
                    p.setFirstName(full_name);
                }
            }
            String username = obj.optString("username");
            p.setProfileUrl(VIEW_PROFILE_URL + username);
            p.setProfileImageURL(obj.optString("profile_picture"));
            plist.add(p);
        }
    }
    return plist;
}

From source file:org.brickred.socialauth.provider.InstagramImpl.java

private Profile getProfile() throws Exception {
    LOG.debug("Obtaining user profile");
    Response response;//from  ww  w  . j  a  v  a2  s. c om
    try {
        response = authenticationStrategy.executeFeed(PROFILE_URL);
    } catch (Exception e) {
        throw new SocialAuthException("Failed to retrieve the user profile from " + PROFILE_URL, e);
    }

    if (response.getStatus() == 200) {
        String respStr = response.getResponseBodyAsString(Constants.ENCODING);
        LOG.debug("Profile JSON string :: " + respStr);
        JSONObject obj = new JSONObject(respStr);
        JSONObject data = obj.getJSONObject("data");
        Profile p = new Profile();
        p.setValidatedId(data.optString("id"));
        String full_name = data.optString("full_name");
        p.setDisplayName(full_name);
        if (full_name != null) {
            String[] names = full_name.split(" ");
            if (names.length > 1) {
                p.setFirstName(names[0]);
                p.setLastName(names[1]);
            } else {
                p.setFirstName(full_name);
            }
        }
        p.setProfileImageURL(data.optString("profile_picture"));
        p.setProviderId(getProviderId());
        return p;
    } else {
        throw new SocialAuthException("Failed to retrieve the user profile from " + PROFILE_URL
                + ". Server response " + response.getStatus());
    }
}

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

@Override
public Pair<JSONObject, byte[]> splitRaw(JSONObject json) {
    byte[] raw = Base64.decode(json.optString(DATA), Base64.DEFAULT);
    json.remove(DATA);/*from   ww w .j av  a2  s .c  o  m*/
    return new Pair<JSONObject, byte[]>(json, raw);
}

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

@Override
public Pair<JSONObject, byte[]> handleOutgoing(JSONObject json) {
    byte[] bytes = Base64.decode(json.optString(DATA), Base64.DEFAULT);
    json.remove(DATA);// www  .  ja  v  a2s  . co  m
    return new Pair<JSONObject, byte[]>(json, bytes);
}

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

@Override
public Pair<JSONObject, byte[]> handleUnprocessed(Context context, JSONObject msg) {
    byte[] bytes = FastBase64.decode(msg.optString(DATA));
    msg.remove(DATA);/*from w  ww. j  a  v  a2 s. com*/
    return new Pair<JSONObject, byte[]>(msg, bytes);
}

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

/**
 * Fills a community object from JSONObject
 * @param from JSONObject describes community object according with VK Docs.
 *//* ww  w .j ava2 s  .  co  m*/
public VKApiCommunity parse(JSONObject from) {
    super.parse(from);
    name = from.optString("name");
    screen_name = from.optString("screen_name", String.format("club%d", Math.abs(id)));
    is_closed = from.optInt("is_closed");
    is_admin = ParseUtils.parseBoolean(from, "is_admin");
    admin_level = from.optInt("admin_level");
    is_member = ParseUtils.parseBoolean(from, "is_member");

    photo_50 = from.optString("photo_50", PHOTO_50);
    if (!TextUtils.isEmpty(photo_50)) {
        photo.add(VKApiPhotoSize.create(photo_50, 50));
    }
    photo_100 = from.optString("photo_100", PHOTO_100);
    if (!TextUtils.isEmpty(photo_100)) {
        photo.add(VKApiPhotoSize.create(photo_100, 100));
    }
    photo_200 = from.optString("photo_200", null);
    if (!TextUtils.isEmpty(photo_200)) {
        photo.add(VKApiPhotoSize.create(photo_200, 200));
    }
    photo.sort();

    String type = from.optString("type", "group");
    if (TYPE_GROUP.equals(type)) {
        this.type = Type.GROUP;
    } else if (TYPE_PAGE.equals(type)) {
        this.type = Type.PAGE;
    } else if (TYPE_EVENT.equals(type)) {
        this.type = Type.EVENT;
    }
    return this;
}

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

@Override
public void handleDirectMessage(Context context, Contact from, JSONObject obj) {
    String packageName = obj.optString(PACKAGE_NAME);
    String arg = obj.optString(ARG);
    Intent launch = new Intent();
    launch.setAction(Intent.ACTION_MAIN);
    launch.addCategory(Intent.CATEGORY_LAUNCHER);
    launch.putExtra(AppState.EXTRA_APPLICATION_ARGUMENT, arg);
    launch.putExtra("creator", false);
    launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    launch.setPackage(packageName);//from   w w  w. j  a  v a2  s  . c  o  m
    final PackageManager mgr = context.getPackageManager();
    List<ResolveInfo> resolved = mgr.queryIntentActivities(launch, 0);
    if (resolved == null || resolved.size() == 0) {
        Toast.makeText(context, "Could not find application to handle invite", Toast.LENGTH_SHORT).show();
        return;
    }
    ActivityInfo info = resolved.get(0).activityInfo;
    launch.setComponent(new ComponentName(info.packageName, info.name));
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launch,
            PendingIntent.FLAG_CANCEL_CURRENT);

    (new PresenceAwareNotify(context)).notify("New Invitation", "Invitation received from " + from.name,
            "Click to launch application.", contentIntent);
}

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

public void render(final Context context, final ViewGroup frame, Obj obj, boolean allowInteractions) {
    JSONObject content = obj.getJson();
    // TODO: hack to show object history in app feeds
    SignedObj appState = getAppStateForChildFeed(context, obj);
    if (appState != null) {
        mAppStateObj.render(context, frame, obj, allowInteractions);
        return;/* w  ww  .  j av a  2  s  . c o  m*/
    } else {
        String appName = content.optString(PACKAGE_NAME);
        if (appName.contains(".")) {
            // TODO: look up label.
            appName = appName.substring(appName.lastIndexOf(".") + 1);
        }
        String text = "Welcome to " + appName + "!";
        TextView valueTV = new TextView(context);
        valueTV.setText(text);
        valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        valueTV.setGravity(Gravity.TOP | Gravity.LEFT);
        frame.addView(valueTV);
    }
}

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

private SignedObj getAppStateForChildFeed(Context context, Obj appReferenceObj) {
    JSONObject appReference = appReferenceObj.getJson();
    if (DBG)/* w  w w.j a  va 2 s .c om*/
        Log.w(TAG, "returning app state for " + appReference.toString());
    if (appReference.has(DbObject.CHILD_FEED_NAME)) {
        String feedName = appReference.optString(DbObject.CHILD_FEED_NAME);
        Uri feedUri = Feed.uriForName(feedName);
        String selection = "type in ('" + AppStateObj.TYPE + "')";
        String[] projection = null;
        String order = "_id desc LIMIT 1";
        Cursor c = context.getContentResolver().query(feedUri, projection, selection, null, order);
        if (c.moveToFirst()) {
            return App.instance().getMusubi().objForCursor(c);
        }
    } else if (appReference.has("state")) {
        return (SignedObj) appReferenceObj;
    }
    return null;
}

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

/**
 * Subscribe to the application feed automatically.
 * TODO, work out observers vs. players.
 */// www. j  a  va  2s.  c om
@Override
public void afterDbInsertion(Context context, DbObj obj) {
    JSONObject content = obj.getJson();
    if (content.has(DbObject.CHILD_FEED_NAME)) {
        String feedName = content.optString(DbObject.CHILD_FEED_NAME);
        DBHelper helper = DBHelper.getGlobal(context);
        Maybe<Group> mg = helper.groupByFeedName(feedName);
        helper.close();
        if (!mg.isKnown() && content.has(GROUP_URI)) {
            Uri gUri = Uri.parse(content.optString(GROUP_URI));
            Group.join(context, gUri);
        }
    }
}