Example usage for org.json JSONArray getString

List of usage examples for org.json JSONArray getString

Introduction

In this page you can find the example usage for org.json JSONArray getString.

Prototype

public String getString(int index) throws JSONException 

Source Link

Document

Get the string associated with an index.

Usage

From source file:com.android.i18n.addressinput.ClientData.java

/**
 * Returns the contents of the JSON-format string as a map.
 *//*from w  w  w  . j  a  v a2s . c  o m*/
protected AddressVerificationNodeData createNodeData(JsoMap jso) {
    Map<AddressDataKey, String> map = new EnumMap<AddressDataKey, String>(AddressDataKey.class);

    JSONArray arr = jso.getKeys();
    for (int i = 0; i < arr.length(); i++) {
        try {
            AddressDataKey key = AddressDataKey.get(arr.getString(i));

            if (key == null) {
                // Not all keys are supported by Android, so we continue if we encounter one
                // that is not used.
                continue;
            }

            String value = jso.get(key.toString().toLowerCase());
            map.put(key, value);
        } catch (JSONException e) {
            // This should not happen - we should not be fetching a key from outside the bounds
            // of the array.
        }
    }

    return new AddressVerificationNodeData(map);
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Query the database using the search term to build up a list of contact ID's 
 * matching the search term/* ww  w. j a va 2 s  .  com*/
 * @param fields
 * @param searchTerm
 * @return a set of contact ID's
 */
private Set<String> buildSetOfContactIds(JSONArray fields, String searchTerm) {
    Set<String> contactIds = new HashSet<String>();

    String key;
    try {
        for (int i = 0; i < fields.length(); i++) {
            key = fields.getString(i);
            if (key.startsWith("displayName")) {
                doQuery(searchTerm, contactIds, People.CONTENT_URI, People._ID, dbMap.get(key) + " LIKE ?",
                        new String[] { searchTerm });
            }
            //            else if (key.startsWith("name")) {
            //               Log.d(LOG_TAG, "Doing " + key + " query");
            //               doQuery(searchTerm, contactIds,
            //                     ContactsContract.Data.CONTENT_URI,
            //                     ContactsContract.Data.CONTACT_ID,
            //                     dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
            //                     new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
            //            }
            else if (key.startsWith("phoneNumbers")) {
                doQuery(searchTerm, contactIds, Phones.CONTENT_URI, Phones.PERSON_ID,
                        dbMap.get(key) + " LIKE ?", new String[] { searchTerm });
            } else if (key.startsWith("emails")) {
                doQuery(searchTerm, contactIds, ContactMethods.CONTENT_EMAIL_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
                        new String[] { searchTerm, ContactMethods.CONTENT_EMAIL_ITEM_TYPE });
            } else if (key.startsWith("addresses")) {
                doQuery(searchTerm, contactIds, ContactMethods.CONTENT_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
                        new String[] { searchTerm, ContactMethods.CONTENT_POSTAL_ITEM_TYPE });
            } else if (key.startsWith("ims")) {
                doQuery(searchTerm, contactIds, ContactMethods.CONTENT_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
                        new String[] { searchTerm, ContactMethods.CONTENT_IM_ITEM_TYPE });
            } else if (key.startsWith("organizations")) {
                doQuery(searchTerm, contactIds, Organizations.CONTENT_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ?", new String[] { searchTerm });
            } else if (key.startsWith("note")) {
                doQuery(searchTerm, contactIds, People.CONTENT_URI, People._ID, dbMap.get(key) + " LIKE ?",
                        new String[] { searchTerm });
            }
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }

    return contactIds;
}

From source file:org.jsharkey.grouphome.LauncherActivity.java

protected String resolveGroup(JSONObject groupMap, String packageName) {
    try {/*from ww  w .  j a va  2  s  .c o m*/
        for (Iterator keys = groupMap.keys(); keys.hasNext();) {
            String groupName = (String) keys.next();
            JSONArray packages = groupMap.getJSONArray(groupName);
            for (int i = 0; i < packages.length(); i++) {
                if (packageName.equals(packages.getString(i)))
                    return groupName;
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Problem while trying to resolve group", e);
    }
    return GROUP_UNKNOWN;
}

From source file:com.liferay.mobile.android.v7.mbmessage.MBMessageService.java

public String getCategoryMessagesRss(long groupId, long categoryId, int status, int max, String type,
        double version, String displayStyle, String feedURL, String entryURL, JSONObjectWrapper themeDisplay)
        throws Exception {
    JSONObject _command = new JSONObject();

    try {// www.  j a  v a2s .  c  om
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("categoryId", categoryId);
        _params.put("status", status);
        _params.put("max", max);
        _params.put("type", checkNull(type));
        _params.put("version", version);
        _params.put("displayStyle", checkNull(displayStyle));
        _params.put("feedURL", checkNull(feedURL));
        _params.put("entryURL", checkNull(entryURL));
        mangleWrapper(_params, "themeDisplay", "com.liferay.portal.kernel.theme.ThemeDisplay", themeDisplay);

        _command.put("/mbmessage/get-category-messages-rss", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getString(0);
}

From source file:com.liferay.mobile.android.v7.mbmessage.MBMessageService.java

public String getCompanyMessagesRss(long companyId, int status, int max, String type, double version,
        String displayStyle, String feedURL, String entryURL, JSONObjectWrapper themeDisplay) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from   ww  w .  j  av  a  2  s  . com
        JSONObject _params = new JSONObject();

        _params.put("companyId", companyId);
        _params.put("status", status);
        _params.put("max", max);
        _params.put("type", checkNull(type));
        _params.put("version", version);
        _params.put("displayStyle", checkNull(displayStyle));
        _params.put("feedURL", checkNull(feedURL));
        _params.put("entryURL", checkNull(entryURL));
        mangleWrapper(_params, "themeDisplay", "com.liferay.portal.kernel.theme.ThemeDisplay", themeDisplay);

        _command.put("/mbmessage/get-company-messages-rss", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getString(0);
}

From source file:com.liferay.mobile.android.v7.mbmessage.MBMessageService.java

public String getGroupMessagesRss(long groupId, int status, int max, String type, double version,
        String displayStyle, String feedURL, String entryURL, JSONObjectWrapper themeDisplay) throws Exception {
    JSONObject _command = new JSONObject();

    try {//w ww  . j  a v a 2 s . com
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("status", status);
        _params.put("max", max);
        _params.put("type", checkNull(type));
        _params.put("version", version);
        _params.put("displayStyle", checkNull(displayStyle));
        _params.put("feedURL", checkNull(feedURL));
        _params.put("entryURL", checkNull(entryURL));
        mangleWrapper(_params, "themeDisplay", "com.liferay.portal.kernel.theme.ThemeDisplay", themeDisplay);

        _command.put("/mbmessage/get-group-messages-rss", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getString(0);
}

From source file:com.liferay.mobile.android.v7.mbmessage.MBMessageService.java

public String getGroupMessagesRss(long groupId, long userId, int status, int max, String type, double version,
        String displayStyle, String feedURL, String entryURL, JSONObjectWrapper themeDisplay) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from   ww w.  j a va  2  s.  c  o  m
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("userId", userId);
        _params.put("status", status);
        _params.put("max", max);
        _params.put("type", checkNull(type));
        _params.put("version", version);
        _params.put("displayStyle", checkNull(displayStyle));
        _params.put("feedURL", checkNull(feedURL));
        _params.put("entryURL", checkNull(entryURL));
        mangleWrapper(_params, "themeDisplay", "com.liferay.portal.kernel.theme.ThemeDisplay", themeDisplay);

        _command.put("/mbmessage/get-group-messages-rss", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getString(0);
}

From source file:com.liferay.mobile.android.v7.mbmessage.MBMessageService.java

public String getThreadMessagesRss(long threadId, int status, int max, String type, double version,
        String displayStyle, String feedURL, String entryURL, JSONObjectWrapper themeDisplay) throws Exception {
    JSONObject _command = new JSONObject();

    try {//from w  w w. j  a va 2  s.  c  om
        JSONObject _params = new JSONObject();

        _params.put("threadId", threadId);
        _params.put("status", status);
        _params.put("max", max);
        _params.put("type", checkNull(type));
        _params.put("version", version);
        _params.put("displayStyle", checkNull(displayStyle));
        _params.put("feedURL", checkNull(feedURL));
        _params.put("entryURL", checkNull(entryURL));
        mangleWrapper(_params, "themeDisplay", "com.liferay.portal.kernel.theme.ThemeDisplay", themeDisplay);

        _command.put("/mbmessage/get-thread-messages-rss", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getString(0);
}

From source file:com.yayandroid.utility.MapHelperFragment.java

/**
 * GoogleApis returns an address as jsonObject according to given
 * coordinate, and here we parse it to find necessary fields
 *//* w w w  . jav  a2  s  . co  m*/
private void fetchInformationUsingGoogleMap() {

    final AndroidHttpClient ANDROID_HTTP_CLIENT = AndroidHttpClient
            .newInstance(MapHelperFragment.class.getName());
    String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + myLocation.getLatitude()
            + "," + myLocation.getLongitude() + "&sensor=false&language=tr";
    try {
        JSONObject googleMapResponse = new JSONObject(
                ANDROID_HTTP_CLIENT.execute(new HttpGet(googleMapUrl), new BasicResponseHandler()));

        // many nested loops.. not great -> use expression instead
        // loop among all results
        JSONArray results = (JSONArray) googleMapResponse.get("results");
        for (int i = 0; i < results.length(); i++) {
            // loop among all addresses within this result
            JSONObject result = results.getJSONObject(i);
            if (result.has("address_components")) {
                JSONArray addressComponents = result.getJSONArray("address_components");

                for (int j = 0; j < addressComponents.length(); j++) {
                    JSONObject addressComponent = addressComponents.getJSONObject(j);
                    if (result.has("types")) {
                        JSONArray types = addressComponent.getJSONArray("types");

                        for (int k = 0; k < requiredInformations.length; k++) {

                            for (int l = 0; l < types.length(); l++) {
                                if (requiredInformations[k].type.value.equals(types.getString(l))) {
                                    if (addressComponent.has("long_name")) {
                                        PostInformation(requiredInformations[k].type,
                                                addressComponent.getString("long_name"));
                                    } else if (addressComponent.has("short_name")) {
                                        PostInformation(requiredInformations[k].type,
                                                addressComponent.getString("short_name"));
                                    }
                                }

                            }

                        }

                    }
                }
            }
        }
    } catch (Exception ignored) {
        ignored.printStackTrace();
    }
    ANDROID_HTTP_CLIENT.close();
}

From source file:de.eorganization.crawler.server.servlets.JSONImportServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    try {//from  w  ww  .  java2 s . c  o  m

        resp.setContentType("text/plain");

        InputStream stream = req.getInputStream();

        log.info("Got Upload " + req.getContentType() + " (" + req.getContentLength() + " bytes) from "
                + req.getRemoteAddr());

        String amiId = req.getHeader("AMI-ID");
        log.info("Got AMI ID:" + amiId);
        String repository = req.getHeader("REPO");
        log.info("Got REPO:" + repository);
        if (amiId == null || repository == null || "".equals(amiId) || "".equals(repository)) {
            log.severe("AMI ID and REPO HTTP Header required.");
            return;
        }

        // Gson gson = new Gson();
        // log.info("Parsed Gson " + gson.fromJson(new
        // InputStreamReader(stream), JsonObject.class));

        String jsonString = IOUtils.toString(stream);
        log.info("Got JSON String "
                + jsonString.substring(0, jsonString.length() > 128 ? 128 : jsonString.length() - 1));
        JSONObject json = new JSONObject();
        try {
            json = new JSONObject(jsonString);
            log.fine("Parsed Json " + json);
            log.finer("Json has keys: " + json.names());
        } catch (JSONException e) {
            log.severe("No JSON sent or wrong format.");
            return;
        }

        JSONObject software = json.optJSONObject("software");
        if (software != null) {
            log.finer("Software JSON " + software);

            JSONArray softwareNames = software.names();
            log.finer("Key Array JSON " + softwareNames);
            for (int i = 0; i < softwareNames.length(); i++) {
                log.fine("software " + softwareNames.getString(i) + " with version "
                        + software.getJSONObject(softwareNames.getString(i)).getString("version"));

                Map<String, String> softAttributes = new HashMap<String, String>();
                JSONArray softwareAttributes = software.getJSONObject(softwareNames.getString(i)).names();
                for (int j = 0; j < softwareAttributes.length(); j++)
                    softAttributes.put(softwareAttributes.getString(j),
                            software.getJSONObject(softwareNames.getString(i))
                                    .getString(softwareAttributes.getString(j)));

                Software soft = AmiManager.saveSoftware(amiId, repository, softwareNames.getString(i),
                        software.getJSONObject(softwareNames.getString(i)).getString("version"),
                        softAttributes);
                if (soft != null) {
                    log.fine("Saved/restored software " + soft);
                    // soft.getAttributes().putAll(softAttributes);
                    // AmiManager.updateSoftware(soft);
                    // log.fine("Saved object " + soft);
                } else
                    log.severe("Not able to save software information for given Ami Id " + amiId + "!");

            }
            List<String> names = new ArrayList<String>();
            for (int i = 0; i < softwareNames.length(); i++)
                names.add(softwareNames.getString(i));
            AmiManager.updateSoftwareNames(names);
            log.info("Saved " + softwareNames.length() + " software objects");

        }

        JSONObject languages = json.optJSONObject("languages");
        if (languages != null) {
            log.finer("Languages JSON " + languages);

            JSONArray languagesNames = languages.names();
            log.finer("Key Array JSON " + languagesNames);
            for (int i = 0; i < languagesNames.length(); i++) {
                log.fine("languages " + languagesNames.getString(i) + " with version "
                        + languages.getJSONObject(languagesNames.getString(i)).getString("version"));

                Map<String, String> langAttributes = new HashMap<String, String>();
                JSONArray languageAttributes = languages.getJSONObject(languagesNames.getString(i)).names();
                for (int j = 0; j < languageAttributes.length(); j++)
                    langAttributes.put(languageAttributes.getString(j),
                            languages.getJSONObject(languagesNames.getString(i))
                                    .getString(languageAttributes.getString(j)));

                Language lang = AmiManager.saveLanguage(amiId, repository, languagesNames.getString(i),
                        languages.getJSONObject(languagesNames.getString(i)).getString("version"),
                        langAttributes);
                if (lang != null) {
                    log.fine("Saved/restored programming language " + lang);
                    lang.getAttributes().putAll(langAttributes);
                    AmiManager.updateLanguage(lang);
                    log.fine("Saved object " + lang);
                } else
                    log.severe("Not able to save programming language information for given Ami Id " + amiId
                            + "!");

            }
            log.info("Saved " + languagesNames.length() + " programming language objects");
        }

        resp.getWriter().println(
                "Saving software packages and programming languages for " + amiId + " (" + repository + ").");

    } catch (JSONException e) {
        log.severe("Error while parsing JSON upload" + e.getMessage() + ", trace: " + e.getStackTrace());
        log.throwing(JSONImportServlet.class.getName(), "doPost", e);
        e.printStackTrace();

    } catch (Exception ex) {
        log.severe("Unexpected error" + ex.getMessage() + ", trace: " + ex.getStackTrace());
        log.throwing(JSONImportServlet.class.getName(), "doPost", ex);
        ex.printStackTrace();
    }

    // log.info("returning to referer " + req.getHeader("referer"));
    // resp.sendRedirect(req.getHeader("referer") != null &&
    // !"".equals(req.getHeader("referer")) ? req.getHeader("referer") :
    // "localhost:8088");
}