Search in sources :

Example 81 with JSONObject

use of org.json.JSONObject in project glitch-hq-android by tinyspeck.

the class MailboxDetailFragment method onRequestBack.

@Override
public void onRequestBack(String method, JSONObject response) {
    if (method == "mail.getMessage") {
        m_root.setVisibility(View.VISIBLE);
        if (response != null) {
            JSONObject message = response.optJSONObject("message");
            m_currentMessage = new glitchMail();
            m_currentMessage.id = m_msgId;
            m_currentMessage.sender_tsid = message.optString("sender_tsid");
            m_currentMessage.sender_label = message.optString("sender_label");
            m_currentMessage.sender_avatar = message.optString("sender_avatar");
            m_currentMessage.currants = message.optInt("currants");
            m_currentMessage.text = message.optString("text");
            if (m_currentMessage.text.equalsIgnoreCase("null")) {
                m_currentMessage.text = "";
            }
            // convert to milliseconds
            m_currentMessage.received = message.optLong("delivery_time") * 1000;
            m_currentMessage.is_read = message.optBoolean("is_read");
            m_currentMessage.is_expedited = message.optBoolean("is_expedited");
            JSONArray items = message.optJSONArray("itemstacks");
            // only one item can be attached now
            if (items != null && items.length() == 1) {
                JSONObject item = items.optJSONObject(0);
                m_currentMessage.item = new glitchMailItem();
                m_currentMessage.item.tsid = item.optString("tsid");
                m_currentMessage.item.name = item.optString("label");
                m_currentMessage.item.class_id = item.optString("item_class");
                m_currentMessage.item.desc = item.optString("desc");
                m_currentMessage.item.icon = item.optString("icon_url");
                m_currentMessage.item.count = item.optInt("count");
            }
            setMailboxDetailView();
        }
    } else if (method == "mail.deleteMessage") {
        if (response.optInt("ok") == 1) {
            Util.shortToast(getActivity(), "Deleted");
        }
        ((HomeScreen) getActivity()).getMailboxFragment().removeMessage(m_currentMessage);
        FragmentManager fm = getFragmentManager();
        fm.popBackStack();
    }
    onRequestComplete();
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 82 with JSONObject

use of org.json.JSONObject in project glitch-hq-android by tinyspeck.

the class QuestsFragment method onRequestBack.

@Override
public void onRequestBack(String method, JSONObject response) {
    if (method == "quests.getAll") {
        JSONArray items = response.optJSONArray("todo");
        if (items != null) {
            m_questsList.clear();
            for (int i = 0; i < items.length(); i++) {
                JSONObject item = items.optJSONObject(i);
                glitchQuest q = new glitchQuest();
                q.title = item.optString("title");
                q.desc = item.optString("desc");
                JSONObject reqs = item.optJSONObject("reqs");
                q.reqs = new Vector<glitchQuestRequirement>();
                if (reqs.length() > 0) {
                    Iterator<String> itr = reqs.keys();
                    while (itr.hasNext()) {
                        String key = itr.next();
                        JSONObject req = reqs.optJSONObject(key);
                        glitchQuestRequirement qr = new glitchQuestRequirement();
                        qr.desc = req.optString("desc");
                        qr.isCount = req.optBoolean("is_count");
                        qr.completed = req.optBoolean("completed");
                        qr.gotNum = req.optInt("got_num");
                        qr.needNum = req.optInt("need_num");
                        qr.icon = req.optString("icon", null);
                        q.reqs.add(qr);
                    }
                }
                JSONObject rewards = item.optJSONObject("rewards");
                q.rewards = new glitchQuestRewards();
                q.rewards.favor = new Vector<glitchQuestRewardFavor>();
                q.rewards.recipes = new Vector<glitchQuestRewardRecipe>();
                q.rewards.imagination = rewards.optInt("xp");
                q.rewards.currants = rewards.optInt("currants");
                q.rewards.energy = rewards.optInt("energy");
                q.rewards.mood = rewards.optInt("mood");
                JSONArray favor = rewards.optJSONArray("favor");
                if (favor != null) {
                    for (int j = 0; j < favor.length(); j++) {
                        JSONObject favorGiant = favor.optJSONObject(j);
                        glitchQuestRewardFavor fg = new glitchQuestRewardFavor();
                        fg.giant = favorGiant.optString("giant");
                        fg.giant = fg.giant.substring(0, 1).toUpperCase() + fg.giant.substring(1);
                        fg.points = favorGiant.optInt("points");
                        q.rewards.favor.add(fg);
                    }
                }
                JSONArray recipes = rewards.optJSONArray("recipes");
                if (recipes != null) {
                    for (int j = 0; j < recipes.length(); j++) {
                        JSONObject recipe = recipes.optJSONObject(j);
                        glitchQuestRewardRecipe r = new glitchQuestRewardRecipe();
                        r.label = recipe.optString("label");
                        r.icon = recipe.optString("icon");
                        q.rewards.recipes.add(r);
                    }
                }
                m_questsList.add(q);
            }
        }
        if (m_questsList.size() == 0) {
            ((TextView) m_root.findViewById(R.id.quests_list_message)).setText("");
        }
        InitRefreshTimer();
        showQuestsPage();
    }
    onRequestComplete();
}
Also used : JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) TextView(android.widget.TextView)

Example 83 with JSONObject

use of org.json.JSONObject in project glitch-hq-android by tinyspeck.

the class Sidebar method requestFinished.

public void requestFinished(GlitchRequest request) {
    if (getActivity() == null)
        return;
    if (request != null && request.method != null) {
        JSONObject response = request.response;
        if (response != null) {
            Log.i("response", " method: " + request.method + " response: " + request.response);
            onRequestBack(request.method, response);
        }
    }
}
Also used : JSONObject(org.json.JSONObject)

Example 84 with JSONObject

use of org.json.JSONObject in project Shuttle by timusus.

the class VideoCastControllerFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mCastConsumer = new MyCastConsumer();
    Bundle bundle = getArguments();
    if (bundle == null) {
        return;
    }
    Bundle extras = bundle.getBundle(EXTRAS);
    Bundle mediaWrapper = extras.getBundle(VideoCastManager.EXTRA_MEDIA);
    // Retain this fragment across configuration changes.
    setRetainInstance(true);
    mCastManager.addTracksSelectedListener(this);
    boolean explicitStartActivity = mCastManager.getPreferenceAccessor().getBooleanFromPreference(VideoCastManager.PREFS_KEY_START_ACTIVITY, false);
    if (explicitStartActivity) {
        mIsFresh = true;
    }
    mCastManager.getPreferenceAccessor().saveBooleanToPreference(VideoCastManager.PREFS_KEY_START_ACTIVITY, false);
    mCastController.setNextPreviousVisibilityPolicy(mCastManager.getCastConfiguration().getNextPrevVisibilityPolicy());
    if (extras.getBoolean(VideoCastManager.EXTRA_HAS_AUTH)) {
        if (mIsFresh) {
            mOverallState = OverallState.AUTHORIZING;
            mMediaAuthService = mCastManager.getMediaAuthService();
            handleMediaAuthTask(mMediaAuthService);
            showImage(Utils.getImageUri(mMediaAuthService.getMediaInfo(), 1));
        }
    } else if (mediaWrapper != null) {
        mOverallState = OverallState.PLAYBACK;
        boolean shouldStartPlayback = extras.getBoolean(VideoCastManager.EXTRA_SHOULD_START);
        String customDataStr = extras.getString(VideoCastManager.EXTRA_CUSTOM_DATA);
        JSONObject customData = null;
        if (!TextUtils.isEmpty(customDataStr)) {
            try {
                customData = new JSONObject(customDataStr);
            } catch (JSONException e) {
                LOGE(TAG, "Failed to unmarshalize custom data string: customData=" + customDataStr, e);
            }
        }
        MediaInfo info = Utils.bundleToMediaInfo(mediaWrapper);
        int startPoint = extras.getInt(VideoCastManager.EXTRA_START_POINT, 0);
        onReady(info, shouldStartPlayback && explicitStartActivity, startPoint, customData);
    }
}
Also used : MediaInfo(com.google.android.gms.cast.MediaInfo) JSONObject(org.json.JSONObject) Bundle(android.os.Bundle) JSONException(org.json.JSONException)

Example 85 with JSONObject

use of org.json.JSONObject in project jpHolo by teusink.

the class Globalization method getStringToNumber.

/*
     * @Description: Parses a number formatted as a string according to the client's user preferences and
     * returns the corresponding number.
     * @Return: JSONObject
     *          Object.value {Number}: The parsed number.
     *
     * @throws: GlobalizationError.PARSING_ERROR
    */
private JSONObject getStringToNumber(JSONArray options) throws GlobalizationError {
    JSONObject obj = new JSONObject();
    Number value;
    try {
        //returns Decimal/Currency/Percent instance
        DecimalFormat fmt = getNumberFormatInstance(options);
        value = fmt.parse((String) options.getJSONObject(0).get(NUMBERSTRING));
        return obj.put("value", value);
    } catch (Exception ge) {
        throw new GlobalizationError(GlobalizationError.PARSING_ERROR);
    }
}
Also used : JSONObject(org.json.JSONObject) DecimalFormat(java.text.DecimalFormat) JSONException(org.json.JSONException)

Aggregations

JSONObject (org.json.JSONObject)4075 JSONException (org.json.JSONException)1601 JSONArray (org.json.JSONArray)1156 Test (org.junit.Test)526 IOException (java.io.IOException)456 ArrayList (java.util.ArrayList)402 HashMap (java.util.HashMap)290 Test (org.testng.annotations.Test)175 File (java.io.File)145 Date (java.util.Date)144 ServiceException (org.b3log.latke.service.ServiceException)125 Bundle (android.os.Bundle)109 VolleyError (com.android.volley.VolleyError)104 Map (java.util.Map)96 BufferedReader (java.io.BufferedReader)93 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)92 InputStreamReader (java.io.InputStreamReader)90 List (java.util.List)85 Response (com.android.volley.Response)84 InputStream (java.io.InputStream)73