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.facebook.share.ShareApi.java

private void shareOpenGraphContent(final ShareOpenGraphContent openGraphContent,
        final FacebookCallback<Sharer.Result> callback) {
    // In order to create a new Open Graph action using a custom object that does not already
    // exist (objectID or URL), you must first send a request to post the object and then
    // another to post the action.  If a local image is supplied with the object or action, that
    // must be staged first and then referenced by the staging URL that is returned by that
    // request./*from w w w .j  a v  a 2s  .  c o m*/
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            final JSONObject data = response.getJSONObject();
            final String postId = (data == null ? null : data.optString("id"));
            ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
        }
    };
    final ShareOpenGraphAction action = openGraphContent.getAction();
    final Bundle parameters = action.getBundle();
    this.addCommonParameters(parameters, openGraphContent);
    if (!Utility.isNullOrEmpty(this.getMessage())) {
        parameters.putString("message", this.getMessage());
    }

    final CollectionMapper.OnMapperCompleteListener stageCallback = new CollectionMapper.OnMapperCompleteListener() {
        @Override
        public void onComplete() {
            try {
                handleImagesOnAction(parameters);

                new GraphRequest(AccessToken.getCurrentAccessToken(),
                        getGraphPath(URLEncoder.encode(action.getActionType(), DEFAULT_CHARSET)), parameters,
                        HttpMethod.POST, requestCallback).executeAsync();
            } catch (final UnsupportedEncodingException ex) {
                ShareInternalUtility.invokeCallbackWithException(callback, ex);
            }
        }

        @Override
        public void onError(FacebookException exception) {
            ShareInternalUtility.invokeCallbackWithException(callback, exception);
        }
    };
    this.stageOpenGraphAction(parameters, stageCallback);
}

From source file:com.facebook.share.ShareApi.java

private void shareLinkContent(final ShareLinkContent linkContent,
        final FacebookCallback<Sharer.Result> callback) {
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
        @Override/*from   w  w  w. j  a  va2s  . c  o  m*/
        public void onCompleted(GraphResponse response) {
            final JSONObject data = response.getJSONObject();
            final String postId = (data == null ? null : data.optString("id"));
            ShareInternalUtility.invokeCallbackWithResults(callback, postId, response);
        }
    };
    final Bundle parameters = new Bundle();
    this.addCommonParameters(parameters, linkContent);
    parameters.putString("message", this.getMessage());
    parameters.putString("link", Utility.getUriString(linkContent.getContentUrl()));
    parameters.putString("picture", Utility.getUriString(linkContent.getImageUrl()));
    parameters.putString("name", linkContent.getContentTitle());
    parameters.putString("description", linkContent.getContentDescription());
    parameters.putString("ref", linkContent.getRef());
    new GraphRequest(AccessToken.getCurrentAccessToken(), getGraphPath("feed"), parameters, HttpMethod.POST,
            requestCallback).executeAsync();
}

From source file:com.facebook.share.ShareApi.java

private void stageOpenGraphObject(final ShareOpenGraphObject object,
        final CollectionMapper.OnMapValueCompleteListener onOpenGraphObjectStagedListener) {
    String type = object.getString("type");
    if (type == null) {
        type = object.getString("og:type");
    }//from  w  w w  .  j  a  v a 2 s  .c  om

    if (type == null) {
        onOpenGraphObjectStagedListener
                .onError(new FacebookException("Open Graph objects must contain a type value."));
        return;
    }
    final JSONObject stagedObject = new JSONObject();
    final CollectionMapper.Collection<String> collection = new CollectionMapper.Collection<String>() {
        @Override
        public Iterator<String> keyIterator() {
            return object.keySet().iterator();
        }

        @Override
        public Object get(String key) {
            return object.get(key);
        }

        @Override
        public void set(String key, Object value, CollectionMapper.OnErrorListener onErrorListener) {
            try {
                stagedObject.put(key, value);
            } catch (final JSONException ex) {
                String message = ex.getLocalizedMessage();
                if (message == null) {
                    message = "Error staging object.";
                }
                onErrorListener.onError(new FacebookException(message));
            }
        }
    };
    final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            final FacebookRequestError error = response.getError();
            if (error != null) {
                String message = error.getErrorMessage();
                if (message == null) {
                    message = "Error staging Open Graph object.";
                }
                onOpenGraphObjectStagedListener.onError(new FacebookGraphResponseException(response, message));
                return;
            }
            final JSONObject data = response.getJSONObject();
            if (data == null) {
                onOpenGraphObjectStagedListener.onError(
                        new FacebookGraphResponseException(response, "Error staging Open Graph object."));
                return;
            }
            final String stagedObjectId = data.optString("id");
            if (stagedObjectId == null) {
                onOpenGraphObjectStagedListener.onError(
                        new FacebookGraphResponseException(response, "Error staging Open Graph object."));
                return;
            }
            onOpenGraphObjectStagedListener.onComplete(stagedObjectId);
        }
    };
    final String ogType = type;
    final CollectionMapper.OnMapperCompleteListener onMapperCompleteListener = new CollectionMapper.OnMapperCompleteListener() {
        @Override
        public void onComplete() {
            final String objectString = stagedObject.toString();
            final Bundle parameters = new Bundle();
            parameters.putString("object", objectString);
            try {
                new GraphRequest(AccessToken.getCurrentAccessToken(),
                        getGraphPath("objects/" + URLEncoder.encode(ogType, DEFAULT_CHARSET)), parameters,
                        HttpMethod.POST, requestCallback).executeAsync();
            } catch (final UnsupportedEncodingException ex) {
                String message = ex.getLocalizedMessage();
                if (message == null) {
                    message = "Error staging Open Graph object.";
                }
                onOpenGraphObjectStagedListener.onError(new FacebookException(message));
            }
        }

        @Override
        public void onError(FacebookException exception) {
            onOpenGraphObjectStagedListener.onError(exception);
        }
    };
    stageCollectionValues(collection, onMapperCompleteListener);
}

From source file:com.facebook.share.ShareApi.java

private void stagePhoto(final SharePhoto photo,
        final CollectionMapper.OnMapValueCompleteListener onPhotoStagedListener) {
    final Bitmap bitmap = photo.getBitmap();
    final Uri imageUrl = photo.getImageUrl();
    if ((bitmap != null) || (imageUrl != null)) {
        final GraphRequest.Callback requestCallback = new GraphRequest.Callback() {
            @Override/* w ww .jav  a2 s. c o  m*/
            public void onCompleted(GraphResponse response) {
                final FacebookRequestError error = response.getError();
                if (error != null) {
                    String message = error.getErrorMessage();
                    if (message == null) {
                        message = "Error staging photo.";
                    }
                    onPhotoStagedListener.onError(new FacebookGraphResponseException(response, message));
                    return;
                }
                final JSONObject data = response.getJSONObject();
                if (data == null) {
                    onPhotoStagedListener.onError(new FacebookException("Error staging photo."));
                    return;
                }
                final String stagedImageUri = data.optString("uri");
                if (stagedImageUri == null) {
                    onPhotoStagedListener.onError(new FacebookException("Error staging photo."));
                    return;
                }

                final JSONObject stagedObject = new JSONObject();
                try {
                    stagedObject.put("url", stagedImageUri);
                    stagedObject.put("user_generated", photo.getUserGenerated());
                } catch (final JSONException ex) {
                    String message = ex.getLocalizedMessage();
                    if (message == null) {
                        message = "Error staging photo.";
                    }
                    onPhotoStagedListener.onError(new FacebookException(message));
                    return;
                }
                onPhotoStagedListener.onComplete(stagedObject);
            }
        };
        if (bitmap != null) {
            ShareInternalUtility.newUploadStagingResourceWithImageRequest(AccessToken.getCurrentAccessToken(),
                    bitmap, requestCallback).executeAsync();
        } else {
            try {
                ShareInternalUtility.newUploadStagingResourceWithImageRequest(
                        AccessToken.getCurrentAccessToken(), imageUrl, requestCallback).executeAsync();
            } catch (final FileNotFoundException ex) {
                String message = ex.getLocalizedMessage();
                if (message == null) {
                    message = "Error staging photo.";
                }
                onPhotoStagedListener.onError(new FacebookException(message));
            }
        }
    } else {
        onPhotoStagedListener.onError(new FacebookException("Photos must have an imageURL or bitmap."));
    }
}

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

@Override
public void render(Context context, ViewGroup frame, Obj obj, boolean allowInteractions) {
    JSONObject content = obj.getJson();

    TextView valueTV = new TextView(context);
    valueTV.setText("Join me in '" + content.optString(GROUP_NAME) + "'");
    valueTV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    valueTV.setGravity(Gravity.TOP | Gravity.LEFT);
    frame.addView(valueTV);//from  w  w  w.j  a v  a  2  s .  co m
}

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

@Override
public void activate(Context context, SignedObj obj) {
    JSONObject content = obj.getJson();
    String groupName = content.optString(GROUP_NAME);
    Uri dynUpdateUri = Uri.parse(content.optString(DYN_UPDATE_URI));

    Intent launch = new Intent(Intent.ACTION_VIEW);
    launch.setData(dynUpdateUri);//from   ww  w. ja  v a  2  s  .  c om
    launch.putExtra("type", TYPE);
    launch.putExtra("creator", false);
    launch.putExtra(GROUP_NAME, groupName);
    launch.putExtra(DYN_UPDATE_URI, dynUpdateUri);
    context.startActivity(launch);
}

From source file:net.portalblockz.portalbot.serverdata.JSONConfigManager.java

public void serializeRepos() {
    JSONArray repoArray = configObject.optJSONArray("git-repos");
    if (repoArray != null) {
        for (int i = 0; i < repoArray.length(); i++) {
            JSONObject repoData = repoArray.optJSONObject(i);
            if (repoData != null) {
                String name = repoData.getString("name");
                String dispName = repoData.optString("dispName");
                dispName = (dispName == null || dispName.length() < 1) ? name : dispName;
                List<String> repoChannels = new ArrayList<>();
                for (int n = 0; n < repoData.getJSONArray("channels").length(); n++) {
                    repoChannels.add(repoData.getJSONArray("channels").getString(n).toLowerCase());
                }//from   w w w.j  a  va 2  s  .  c  o  m
                repoMap.put(name.toLowerCase(), repoChannels);
                repoDispNames.put(name.toLowerCase(), dispName);
            }
        }
    }
}

From source file:com.eutectoid.dosomething.picker.FriendPickerFragment.java

/**
 * Sets the list of friends for pre selection. These friends will be selected by default.
 * @param graphUsers list of friends as GraphUsers
 *//*from   w  w  w .j  a va2s  .c o m*/
public void setSelection(List<JSONObject> graphUsers) {
    List<String> userIds = new ArrayList<String>();
    for (JSONObject graphUser : graphUsers) {
        String id = graphUser.optString("id");
        Validate.notNullOrEmpty(id, "id");
        userIds.add(id);
    }
    setSelectionByIds(userIds);
}

From source file:com.eutectoid.dosomething.picker.GraphObjectAdapter.java

protected String getSectionKeyOfGraphObject(JSONObject graphObject) {
    String result = null;/*from  w  w w.j a  v  a2s . co  m*/

    if (groupByField != null) {
        result = graphObject.optString(groupByField);
        if (result != null && result.length() > 0) {
            result = result.substring(0, 1).toUpperCase();
        }
    }

    return (result != null) ? result : "";
}

From source file:com.eutectoid.dosomething.picker.GraphObjectAdapter.java

protected CharSequence getTitleOfGraphObject(JSONObject graphObject) {
    return graphObject.optString(NAME);
}