Example usage for android.database.sqlite SQLiteConstraintException printStackTrace

List of usage examples for android.database.sqlite SQLiteConstraintException printStackTrace

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteConstraintException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.appdynamics.demo.gasp.service.RestaurantSyncService.java

@Override
public void onCompleted(String results) {
    Log.i(TAG, "Response from " + mGaspRestaurantsUri.toString() + " :" + results + '\n');

    if (results != null) {
        try {/* w ww .j  ava  2 s .c om*/
            Gson gson = new Gson();
            Type type = new TypeToken<List<Restaurant>>() {
            }.getType();
            List<Restaurant> restaurants = gson.fromJson(results, type);

            // Check how many records already in local SQLite database
            long localRecords = checkLastId();

            RestaurantDataAdapter restaurantsDB = new RestaurantDataAdapter(getApplicationContext());
            restaurantsDB.open();
            ListIterator<Restaurant> iterator = restaurants.listIterator();
            int index = 0;

            while (iterator.hasNext()) {
                try {
                    Restaurant restaurant = iterator.next();
                    if (restaurant.getId() > localRecords) {
                        restaurantsDB.insert(restaurant);
                        index = restaurant.getId();
                    }
                } catch (SQLiteConstraintException e) {
                    e.printStackTrace();
                }
            }
            restaurantsDB.close();

            String resultTxt = "Sync: Found " + localRecords + ", Loaded " + index + " restaurants from "
                    + mGaspRestaurantsUri;
            Log.i(TAG, resultTxt + '\n');

            // Notify LocationsActivity that Gasp restaurant data has been synced
            LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(LocationsActivity.SYNC_COMPLETED));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}