Example usage for org.json JSONArray getJSONArray

List of usage examples for org.json JSONArray getJSONArray

Introduction

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

Prototype

public JSONArray getJSONArray(int index) throws JSONException 

Source Link

Document

Get the JSONArray associated with an index.

Usage

From source file:com.smedic.tubtub.JsonAsyncTask.java

@Override
protected ArrayList<String> doInBackground(String... params) {

    //encode param to avoid spaces in URL
    String encodedParam = "";
    try {/*from   w w w .  j a  v a 2 s.  c o  m*/
        encodedParam = URLEncoder.encode(params[0], "UTF-8").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    ArrayList<String> items = new ArrayList<>();
    try {
        URL url = new URL(
                "http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=" + encodedParam);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();
        // gets the server json data
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));

        String next;
        while ((next = bufferedReader.readLine()) != null) {

            if (checkJson(next) == JSON_ERROR) {
                //if not valid, remove invalid parts (this is simple hack for URL above)
                next = next.substring(19, next.length() - 1);
            }

            JSONArray ja = new JSONArray(next);

            for (int i = 0; i < ja.length(); i++) {

                if (ja.get(i) instanceof JSONArray) {
                    JSONArray ja2 = ja.getJSONArray(i);

                    for (int j = 0; j < ja2.length(); j++) {

                        if (ja2.get(j) instanceof JSONArray) {
                            String suggestion = ((JSONArray) ja2.get(j)).getString(0);
                            //Log.d(TAG, "Suggestion: " + suggestion);
                            items.add(suggestion);
                        }
                    }
                } else if (ja.get(i) instanceof JSONObject) {
                    //Log.d(TAG, "json object");
                } else {
                    //Log.d(TAG, "unknown object");
                }
            }
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
    return items;
}

From source file:com.optimizely.ab.config.parser.JsonConfigParser.java

private Condition parseConditions(JSONArray conditionJson) {
    List<Condition> conditions = new ArrayList<Condition>();
    String operand = (String) conditionJson.get(0);

    for (int i = 1; i < conditionJson.length(); i++) {
        Object obj = conditionJson.get(i);
        if (obj instanceof JSONArray) {
            conditions.add(parseConditions(conditionJson.getJSONArray(i)));
        } else {/* ww w .  j  a  v  a2  s.  c  om*/
            JSONObject conditionMap = (JSONObject) obj;
            conditions.add(new UserAttribute((String) conditionMap.get("name"),
                    (String) conditionMap.get("type"), (String) conditionMap.get("value")));
        }
    }

    Condition condition;
    if (operand.equals("and")) {
        condition = new AndCondition(conditions);
    } else if (operand.equals("or")) {
        condition = new OrCondition(conditions);
    } else {
        condition = new NotCondition(conditions.get(0));
    }

    return condition;
}

From source file:com.galactogolf.serialization.JSONSerializer.java

private static EntityDefinition fromEntityDefinitionJSON(JSONObject entityDefnJSON) throws JSONException {
    JSONArray positionsJSON = entityDefnJSON.getJSONArray(JSONTags.ENTITY_DEFINITION_POSITION);
    float x = (float) positionsJSON.getJSONArray(0).getDouble(0);
    float y = (float) positionsJSON.getJSONArray(0).getDouble(1);
    EntityDefinition entityDefn = new EntityDefinition(
            entityDefnJSON.getString(JSONTags.ENTITY_DEFINITION_NAME),
            entityDefnJSON.getString(JSONTags.ENTITY_DEFINITION_TYPE), x, y);

    if (positionsJSON.length() > 1) {
        entityDefn.p2 = new Vector2D((float) positionsJSON.getJSONArray(1).getDouble(0),
                (float) positionsJSON.getJSONArray(1).getDouble(1));
    }//from ww  w. ja va 2 s .  co  m

    return entityDefn;
}

From source file:com.liferay.mobile.android.v62.bookmarksentry.BookmarksEntryService.java

public JSONArray getEntries(long groupId, long folderId, int start, int end) throws Exception {
    JSONObject _command = new JSONObject();

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

        _params.put("groupId", groupId);
        _params.put("folderId", folderId);
        _params.put("start", start);
        _params.put("end", end);

        _command.put("/bookmarksentry/get-entries", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

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

    return _result.getJSONArray(0);
}

From source file:com.liferay.mobile.android.v62.bookmarksentry.BookmarksEntryService.java

public JSONArray getEntries(long groupId, long folderId, int start, int end,
        JSONObjectWrapper orderByComparator) throws Exception {
    JSONObject _command = new JSONObject();

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

        _params.put("groupId", groupId);
        _params.put("folderId", folderId);
        _params.put("start", start);
        _params.put("end", end);
        mangleWrapper(_params, "orderByComparator", "com.liferay.portal.kernel.util.OrderByComparator",
                orderByComparator);

        _command.put("/bookmarksentry/get-entries", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

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

    return _result.getJSONArray(0);
}

From source file:com.liferay.mobile.android.v62.bookmarksentry.BookmarksEntryService.java

public JSONArray getGroupEntries(long groupId, int start, int end) throws Exception {
    JSONObject _command = new JSONObject();

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

        _params.put("groupId", groupId);
        _params.put("start", start);
        _params.put("end", end);

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

    JSONArray _result = session.invoke(_command);

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

    return _result.getJSONArray(0);
}

From source file:com.liferay.mobile.android.v62.bookmarksentry.BookmarksEntryService.java

public JSONArray getGroupEntries(long groupId, long userId, int start, int end) throws Exception {
    JSONObject _command = new JSONObject();

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

        _params.put("groupId", groupId);
        _params.put("userId", userId);
        _params.put("start", start);
        _params.put("end", end);

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

    JSONArray _result = session.invoke(_command);

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

    return _result.getJSONArray(0);
}

From source file:com.liferay.mobile.android.v62.bookmarksentry.BookmarksEntryService.java

public JSONArray getGroupEntries(long groupId, long userId, long rootFolderId, int start, int end)
        throws Exception {
    JSONObject _command = new JSONObject();

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

        _params.put("groupId", groupId);
        _params.put("userId", userId);
        _params.put("rootFolderId", rootFolderId);
        _params.put("start", start);
        _params.put("end", end);

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

    JSONArray _result = session.invoke(_command);

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

    return _result.getJSONArray(0);
}

From source file:com.norman0406.slimgress.API.Game.GameState.java

public void intLoadCommunication(final double radiusKM, final boolean factionOnly, final Handler handler) {
    try {//  w w w.  j av  a 2s.  c o  m
        checkInterface();

        final double earthKM = 2 * Math.PI * 6371; // circumference

        S2LatLng center = S2LatLng.fromE6(mLocation.getLatitude(), mLocation.getLongitude());
        S2LatLng size = S2LatLng.fromRadians((Math.PI / earthKM) * radiusKM,
                (2 * Math.PI / earthKM) * radiusKM);
        S2LatLngRect region = S2LatLngRect.fromCenterSize(center, size);

        // get cell ids for area
        String[] cellIds = Utils.getCellIdsFromRegion(region, 8, 12);

        // create cells
        JSONArray cellsAsHex = new JSONArray();
        for (int i = 0; i < cellIds.length; i++)
            cellsAsHex.put(cellIds[i]);

        // create params
        JSONObject params = new JSONObject();
        params.put("cellsAsHex", cellsAsHex);
        params.put("minTimestampMs", -1);
        params.put("maxTimestampMs", -1);
        params.put("desiredNumItems", 50);
        params.put("factionOnly", factionOnly);
        params.put("ascendingTimestampOrder", false);

        mInterface.request(mHandshake, "playerUndecorated/getPaginatedPlexts", mLocation, params,
                new RequestResult(handler) {
                    @Override
                    public void handleResult(JSONArray result) {
                        try {
                            // add plexts
                            for (int i = 0; i < result.length(); i++) {
                                PlextBase newPlext = PlextBase.createByJSON(result.getJSONArray(i));
                                mPlexts.add(newPlext);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.mobile.android.v62.ddmstructure.DDMStructureService.java

public JSONArray getStructures(long groupId) throws Exception {
    JSONObject _command = new JSONObject();

    try {/*from   w ww  .  ja  va2 s  .co m*/
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);

        _command.put("/ddmstructure/get-structures", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

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

    return _result.getJSONArray(0);
}