Esempio n. 1
0
  /** parse return string from request and insert into database* */
  private void GetReviewsDataFromJson(String movieJsonStr, String movieId) throws JSONException {

    // These are the names of the JSON objects that need to be extracted.
    final String Array_RESULT = "results";
    final String REVIEW_AUTHOR = "author";
    final String REVIEW_CONTENT = "content";

    JSONObject movieJson = new JSONObject(movieJsonStr);
    JSONArray movieArray = movieJson.getJSONArray(Array_RESULT);

    // Insert the new trailers information into the database
    Vector<ContentValues> cVVector = new Vector<ContentValues>(movieArray.length());

    for (int i = 0; i < movieArray.length(); i++) {
      // Get the JSON object representing the time
      JSONObject movieObject = movieArray.getJSONObject(i);

      // get data from Object and append to content Values Vector
      ContentValues moviesValues = new ContentValues();

      moviesValues.put(MoviesContract.COLUMN_MOV_ID, movieId);
      moviesValues.put(
          MoviesContract.ReviewsEntry.COLUMN_REV_AUTHOR, movieObject.getString(REVIEW_AUTHOR));
      moviesValues.put(
          MoviesContract.ReviewsEntry.COLUMN_REV_CONTENT, movieObject.getString(REVIEW_CONTENT));

      cVVector.add(moviesValues);
    }

    // build uri of Reviews and review with id
    Uri reviewUri = MoviesContract.ReviewsEntry.CONTENT_URI;
    Uri reviewWithIdUri =
        MoviesContract.ReviewsEntry.buildReviewUriWithMovieId(Long.valueOf(movieId));

    int deleted = 0;
    // delete data from database
    deleted = mContext.getContentResolver().delete(reviewWithIdUri, null, null);
    Log.d("Reviews Row Deleted ", String.valueOf(deleted));

    int inserted = 0;
    // add to database
    if (cVVector.size() > 0) {
      ContentValues[] cvArray = new ContentValues[cVVector.size()];
      cVVector.toArray(cvArray);
      inserted = mContext.getContentResolver().bulkInsert(reviewUri, cvArray);
    }
    Log.d("Review Row Inserted ", String.valueOf(inserted));
  }