Example usage for org.json JSONArray length

List of usage examples for org.json JSONArray length

Introduction

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

Prototype

public int length() 

Source Link

Document

Get the number of elements in the JSONArray, included nulls.

Usage

From source file:hongik.android.project.best.StoreReviewActivity.java

public void drawTable() throws Exception {
    String query = "func=morereview" + "&license=" + license;
    DBConnector conn = new DBConnector(query);
    conn.start();/*from w  w w  . j  a v  a 2 s  .c  o m*/

    conn.join();
    JSONObject jsonResult = conn.getResult();
    boolean result = jsonResult.getBoolean("result");

    if (!result) {
        return;
    }

    String storeName = jsonResult.getString("sname");
    ((TextViewPlus) findViewById(R.id.storereview_storename)).setText(storeName);

    JSONArray review = null;
    if (!jsonResult.isNull("review")) {
        review = jsonResult.getJSONArray("review");
    }

    //Draw Review Table
    if (review != null) {
        TableRow motive = (TableRow) reviewTable.getChildAt(1);

        for (int i = 0; i < review.length(); i++) {
            JSONObject json = review.getJSONObject(i);
            final String[] elements = new String[4];
            elements[0] = Double.parseDouble(json.getString("GRADE")) + "";
            elements[1] = json.getString("NOTE");
            elements[2] = json.getString("CID#");
            elements[3] = json.getString("DAY");

            TableRow tbRow = new TableRow(this);
            TextViewPlus[] tbCols = new TextViewPlus[4];

            if (elements[1].length() > 14)
                elements[1] = elements[1].substring(0, 14) + "...";

            for (int j = 0; j < 4; j++) {
                tbCols[j] = new TextViewPlus(this);
                tbCols[j].setText(elements[j]);
                tbCols[j].setLayoutParams(motive.getChildAt(j).getLayoutParams());
                tbCols[j].setGravity(Gravity.CENTER);
                tbCols[j].setTypeface(Typeface.createFromAsset(tbCols[j].getContext().getAssets(),
                        "InterparkGothicBold.ttf"));
                tbCols[j].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent reviewIntent = new Intent(originActivity, ReviewDetailActivity.class);
                        reviewIntent.putExtra("ACCESS", "STORE");
                        reviewIntent.putExtra("CID", elements[2]);
                        reviewIntent.putExtra("LICENSE", license);
                        Log.i("StoreReview", "StartActivity");
                        startActivity(reviewIntent);
                    }
                });

                Log.i("StoreMenu", "COL" + j + ":" + elements[j]);
                tbRow.addView(tbCols[j]);
            }
            reviewTable.addView(tbRow);
        }
    }
    reviewTable.removeViewAt(1);
}

From source file:de.duenndns.ssl.MemorizingTrustManager.java

private List<String> getPoshFingerprintsFromServer(String domain, String url, int maxTtl, boolean followUrl) {
    Log.d("mtm", "downloading json for " + domain + " from " + url);
    try {/*from   www .  j a  va2 s . co  m*/
        List<String> results = new ArrayList<>();
        HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder builder = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            builder.append(inputLine);
        }
        JSONObject jsonObject = new JSONObject(builder.toString());
        in.close();
        int expires = jsonObject.getInt("expires");
        if (expires <= 0) {
            return new ArrayList<>();
        }
        if (maxTtl >= 0) {
            expires = Math.min(maxTtl, expires);
        }
        String redirect;
        try {
            redirect = jsonObject.getString("url");
        } catch (JSONException e) {
            redirect = null;
        }
        if (followUrl && redirect != null && redirect.toLowerCase().startsWith("https")) {
            return getPoshFingerprintsFromServer(domain, redirect, expires, false);
        }
        JSONArray fingerprints = jsonObject.getJSONArray("fingerprints");
        for (int i = 0; i < fingerprints.length(); i++) {
            JSONObject fingerprint = fingerprints.getJSONObject(i);
            String sha256 = fingerprint.getString("sha-256");
            if (sha256 != null) {
                results.add(sha256);
            }
        }
        writeFingerprintsToCache(domain, results, 1000L * expires + System.currentTimeMillis());
        return results;
    } catch (Exception e) {
        Log.d("mtm", "error fetching posh " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:de.duenndns.ssl.MemorizingTrustManager.java

private List<String> getPoshFingerprintsFromCache(String domain) {
    File file = getPoshCacheFile(domain);
    try {//  w  ww.j a  va 2  s  .co m
        InputStream is = new FileInputStream(file);
        BufferedReader buf = new BufferedReader(new InputStreamReader(is));

        String line = buf.readLine();
        StringBuilder sb = new StringBuilder();

        while (line != null) {
            sb.append(line).append("\n");
            line = buf.readLine();
        }
        JSONObject jsonObject = new JSONObject(sb.toString());
        is.close();
        long expires = jsonObject.getLong("expires");
        long expiresIn = expires - System.currentTimeMillis();
        if (expiresIn < 0) {
            file.delete();
            return null;
        } else {
            Log.d("mtm", "posh fingerprints expire in " + (expiresIn / 1000) + "s");
        }
        List<String> result = new ArrayList<>();
        JSONArray jsonArray = jsonObject.getJSONArray("fingerprints");
        for (int i = 0; i < jsonArray.length(); ++i) {
            result.add(jsonArray.getString(i));
        }
        return result;
    } catch (FileNotFoundException e) {
        return null;
    } catch (IOException e) {
        return null;
    } catch (JSONException e) {
        file.delete();
        return null;
    }
}

From source file:org.ESLM.Parser.JSONProtoLessonParser.java

private Instruction[] parseExercises(JSONArray jsonExercises) throws BadFormedJSONExerciseException {
    int numberOfExercises = jsonExercises.length();
    Instruction[] instructions = new Instruction[numberOfExercises];
    for (int i = 0; i < numberOfExercises; i++) {
        JSONObject jsonExercise = jsonExercises.getJSONObject(i);
        instructions[i] = ExerciseFactory.createInstruction(jsonExercise);
    }/*from w ww  .  j  a va 2  s.  co m*/
    return instructions;
}

From source file:nl.hnogames.domoticzapi.Parsers.CameraParser.java

@Override
public void parseResult(String result) {
    try {//  w ww .j  a v  a2 s.c o m
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<CameraInfo> mCameras = new ArrayList<>();
        if (jsonArray.length() > 0) {
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject row = jsonArray.getJSONObject(i);
                CameraInfo cameraInfo = new CameraInfo(row);
                cameraInfo.setSnapShotURL(this.domoticz.getSnapshotUrl(cameraInfo));
                if (cameraInfo.getEnabled())
                    mCameras.add(cameraInfo);
            }
        }
        receiver.OnReceiveCameras(mCameras);
    } catch (JSONException e) {
        Log.e(TAG, "CameraParser JSON exception");
        e.printStackTrace();
        receiver.onError(e);
    }
}

From source file:com.nextgis.maplib.datasource.ngw.LayerWithStyles.java

public void fillStyles() {
    mStyles = new ArrayList<>();
    try {//from w  w w .j  a va  2s  .c o m
        String sURL = mConnection.getURL() + "/resource/" + mRemoteId + "/child/";
        HttpGet get = new HttpGet(sURL);
        get.setHeader("Cookie", mConnection.getCookie());
        get.setHeader("Accept", "*/*");
        HttpResponse response = mConnection.getHttpClient().execute(get);
        HttpEntity entity = response.getEntity();
        JSONArray children = new JSONArray(EntityUtils.toString(entity));
        for (int i = 0; i < children.length(); i++) {
            //Only store style id
            //To get more style properties need to create style class extended from Resource
            //Style extends Resource
            //mStyles.add(new Style(styleObject, mConnection);
            JSONObject styleObject = children.getJSONObject(i);
            JSONObject JSONResource = styleObject.getJSONObject("resource");
            long remoteId = JSONResource.getLong("id");
            mStyles.add(remoteId);
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    }
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a JSON array stored at the provided key from the provided JSON array
 * @param array The JSON object to retrieve from
 * @param key The key to retrieve/*www.ja  v a  2 s  .  co  m*/
 * @return the array stored in the key, or null if the key doesn't exist
 */
public static JSONArray safeGetArrayFromArray(JSONArray array, int index) {
    if (array == null || index < 0)
        return null;
    if (index < array.length()) {
        try {
            return array.getJSONArray(index);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get Array from JSONArray at index " + index, e);
        }
    }
    return null;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a double stored at the provided index from the provided JSON array
 * @param obj The JSON array to retrieve from
 * @param index The index to retrieve the double from
 * @return the double stored at the index, or the default value if the index doesn't exist
 *//*  www  . j a v  a 2s  .co m*/
public static double safeGetDoubleFromArray(JSONArray obj, int index, double defaultValue) {
    if (obj != null && obj.length() > index) {
        try {
            return obj.getDouble(index);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get string from JSONArray from at index " + index, e);
        }
    }
    return defaultValue;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a JSON object stored at the provided index from the provided JSON Array object
 * @param obj The JSON Array object to retrieve from
 * @param key The index to retrieve//  w  ww. jav a 2 s.  c  o  m
 * @return the JSON object stored at the index, or null if the index doesn't exist
 */
public static JSONObject safeGetJSONObjectFromArray(JSONArray obj, int index) {
    if (obj == null || index < 0)
        return null;
    if (obj.length() > index) {
        try {
            return obj.getJSONObject(index);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get object from JSONArray from at index " + index, e);
        }
    }
    return null;
}

From source file:com.kevinquan.android.utils.JSONUtils.java

/**
 * Retrieve a string stored at the provided index from the provided JSON array
 * @param obj The JSON array to retrieve from
 * @param index The index to retrieve the string from
 * @return the string stored at the index, or null if the index doesn't exist
 *//*from w ww .  j a  v a 2 s.c o  m*/
public static String safeGetStringFromArray(JSONArray obj, int index) {
    if (obj != null && obj.length() > index) {
        try {
            return obj.getString(index);
        } catch (JSONException e) {
            Log.w(TAG, "Could not get string from JSONArray from at index " + index, e);
        }
    }
    return null;
}