Example usage for android.util JsonReader setLenient

List of usage examples for android.util JsonReader setLenient

Introduction

In this page you can find the example usage for android.util JsonReader setLenient.

Prototype

public void setLenient(boolean lenient) 

Source Link

Document

Configure this parser to be be liberal in what it accepts.

Usage

From source file:watch.oms.omswatch.parser.OMSConfigDBParser.java

/**
 * Parses Service response and stores into respective DB table.
 * //  www .ja  v a 2  s  . co  m
 * @param pStringReader
 */
private void readJsonStream(Reader pStringReader) {
    double latestModifiedTimeStamp = 0.0f;
    JsonReader reader = null;
    List<ContentValues> rows = null;
    String tableName = null;
    ExecutorService executor = Executors.newFixedThreadPool(10);
    final String VISITED_DATE = "visiteddate";
    OMSServerMapperHelper servermapperhelper = new OMSServerMapperHelper();
    final String DB_PROCESS_DURATION = "dbprocessduration";
    final String SERVER_PROCESS_DURATION = "serverprocessduration";

    try {
        Log.d(TAG, "@@@@@@@@@@ Config DB Tables Start @@@@@@@@@@");
        reader = new JsonReader(pStringReader);
        reader.setLenient(true);
        reader.beginObject();
        // Iterate through each table data
        while (reader.hasNext()) {
            tableName = reader.nextName();
            if (tableName.equals(VISITED_DATE)) {

                latestModifiedTimeStamp = reader.nextDouble();

                /*servermapperhelper.updateModifiedTimeStampForAppsTable(
                       latestModifiedTimeStamp);*/
                /*if (Integer.parseInt(OMSApplication
                      .getInstance().getAppId()) == 10) {
                   servermapperhelper
                .updateModifiedTimeStampForVisitedDateMapper(
                      OMSApplication
                            .getInstance()
                            .getEditTextHiddenVal(),
                      latestModifiedTimeStamp);
                }*/
                continue;
            }
            if (tableName.equals(OMSConstants.NULL_STRING)) {
                continue;
            }
            //Fetch dbprocess duration serverprocess duration
            else if (DB_PROCESS_DURATION.equalsIgnoreCase(tableName)) {
                String dbDuration = reader.nextString();
                OMSApplication.getInstance().setDatabaseProcessDuration(dbDuration);
                Log.i(TAG, "DB Process Duration" + dbDuration);
                continue;
            }
            if (SERVER_PROCESS_DURATION.equalsIgnoreCase(tableName)) {
                String serverProcessDuration = reader.nextString();
                OMSApplication.getInstance().setServerProcessDuration(serverProcessDuration);
                Log.i(TAG, "server process duration " + serverProcessDuration);
                continue;
            }
            rows = readAllRowDataForTable(reader, tableName);

            Runnable worker = new DbWorkerThread(tableName, rows);
            executor.execute(worker);
        }
        reader.endObject();
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        Log.d(TAG, "@@@@@@@@@@ Config DB Tables End @@@@@@@@@@");
        // Update Apps Table
        Log.d(TAG, "@@@@@@@@@@ Updating AppsTable with ConfigLastModifieddate:" + latestModifiedTimeStamp);
        servermapperhelper.updateModifiedTimeStampForAppsTable(latestModifiedTimeStamp);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            Log.e(TAG, "IOException occurred while loading file from Assets folder." + e.getMessage());
            e.printStackTrace();
        }

    }

}

From source file:watch.oms.omswatch.actioncenter.helpers.WatchTransDBParser.java

/**
 * Parses Service response and stores into respective DB table.
 * //  w  w  w.ja  v a2s.c o  m
 * @param pStringReader
 */
private void readJsonStream(Reader pStringReader) {

    JsonReader reader = null;
    List<ContentValues> rows = null;
    String tableName = null;
    String colName = null;
    ExecutorService executor = Executors.newFixedThreadPool(10);
    double latestModifiedTimeStamp = 0.0f;
    final String VISITED_DATE = "visiteddate";
    final String MESSAGE = "message";
    final String ADDITION_MESSAGE = "additionMessage";
    final String VISITED_DATE_MAPPER = "visiteddatemapper";
    List<String> tableNames = new ArrayList<String>();
    final String DB_PROCESS_DURATION = "dbprocessduration";
    final String SERVER_PROCESS_DURATION = "serverprocessduration";
    try {
        Log.d(TAG, "@@@@@@@@@@ Trans DB Tables Start @@@@@@@@@@");
        reader = new JsonReader(pStringReader);
        reader.setLenient(true);
        reader.beginObject();

        // Iterate through each table data
        while (reader.hasNext()) {

            colName = reader.nextName();
            if (colName.equals(VISITED_DATE)) {

                latestModifiedTimeStamp = reader.nextDouble();
                // Update Trans Table
                /*servermapperhelper.updateModifiedTimeStampForTransTable(
                      ALL_TABLES, latestModifiedTimeStamp);*/
                if (Integer.parseInt(OMSApplication.getInstance().getAppId()) == 10) {
                    servermapperhelper.updateModifiedTimeStampForVisitedDateMapper(
                            OMSApplication.getInstance().getEditTextHiddenVal(), latestModifiedTimeStamp);
                }
                continue;
            } else if (colName.equals(MESSAGE)) {
                Log.e(TAG, "Trans DB gave error response - message - " + reader.nextString());
                continue;
            } else if (colName.equals(ADDITION_MESSAGE)) {
                Log.e(TAG, "Trans DB gave error response - additionMessage - " + reader.nextString());
                continue;
            } else if (VISITED_DATE_MAPPER.equalsIgnoreCase(colName)) {
                Log.d(TAG, "Skipping internal Table " + VISITED_DATE_MAPPER + " lookup");
                reader.skipValue();
                continue;
            }
            //Fetch dbprocess duration serverprocess duration
            else if (DB_PROCESS_DURATION.equalsIgnoreCase(colName)) {
                String dbDuration = reader.nextString();
                OMSApplication.getInstance().setDatabaseProcessDuration(dbDuration);
                /*Log.i(TAG,
                      "DB Process Duration"
                   + dbDuration);*/
                continue;
            } else if (SERVER_PROCESS_DURATION.equalsIgnoreCase(colName)) {
                String serverProcessDuration = reader.nextString();
                OMSApplication.getInstance().setServerProcessDuration(serverProcessDuration);
                /*Log.i(TAG,
                      "server process duration "
                   + serverProcessDuration);*/
                continue;
            }
            Log.d(TAG, "ColName::::" + colName);
            // Get Table Name
            tableName = servermapperhelper.getClientTableName(colName);

            if (tableName == null) {
                Log.e(TAG, "Table Name was not found in ServerMapperHelper - " + colName);
                // Tables created only on the server sometimes dont find
                // entry in ServerMapper. So, allowing those tables here
                tableNames.add(colName);
            } else {
                tableNames.add(tableName);
            }

            rows = readAllRowDataForTable(reader, tableName);

            // Update DB only if we have valid Table name
            if (tableName != null) {
                Runnable worker = new DbWorkerThread(colName, rows);
                executor.execute(worker);
            }
        }
        reader.endObject();

        Log.d(TAG, "Waiting for DB Worker Threads to Complete");
        // Request for Shutdown. This will wait till the db updates are
        // complete. Wait till the db update is complete and then invoke the
        // time stamp update to avoid db locks.
        executor.shutdown();
        while (!executor.isTerminated()) {
        }

        Log.d(TAG, "DB Worker Threads Completed");
        // Update Modified Time Stamp for All Trans Tables
        executor = Executors.newFixedThreadPool(1);
        Runnable worker = new DbWorkerThreadToUpdateTimeStamp(tableNames, latestModifiedTimeStamp);
        executor.execute(worker);

        // Request for Shutdown. This will wait till the db updates are
        // complete
        Log.d(TAG, "Waiting for DB Timestamp Update Worker Thread to Complete");
        executor.shutdown();
        while (!executor.isTerminated()) {
        }

        Log.d(TAG, "DB Timestamp Update Worker Thread Completed");
        Log.d(TAG, "@@@@@@@@@@ Trans DB Tables End @@@@@@@@@@");

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}