Advertisement
Ankhwatcher

IntentService

Nov 15th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. @Override
  2.     protected void onHandleIntent(Intent intent) {
  3.         Log.d(this.getClass().getName(), "New intent received.");
  4.         BookProvider bookProvider = new BookProvider();
  5.         bookProvider.setContext(this);
  6.         String url = this.getResources().getString(R.string.api_url) + "books";
  7.  
  8.         InputStream source = retrieveStream(url);
  9.         StringWriter writer = new StringWriter();
  10.         String retrievedJSON = "";
  11.         if (source != null) {
  12.             try {
  13.                 IOUtils.copy(source, writer);
  14.                 retrievedJSON = writer.toString();
  15.             } catch (IOException e) {
  16.                 e.printStackTrace();
  17.             }
  18.         }
  19.  
  20.         if (!retrievedJSON.equals("")) {
  21.             Gson gson = new Gson();
  22.             BookListEntry[] bookListEntries = gson.fromJson(retrievedJSON, BookListEntry[].class);
  23.  
  24.             for (BookListEntry bookListEntry : bookListEntries) {
  25.                 ContentValues contentValues = new ContentValues();
  26.                 contentValues.put(BookOpenHelper.BOOK_ID, bookListEntry.bookId);
  27.                 contentValues.put(BookOpenHelper.BOOK_TITLE, bookListEntry.bookTitle);
  28.                 contentValues.put(BookOpenHelper.BOOK_ISBN, bookListEntry.bookISBN);
  29.                 contentValues.put(BookOpenHelper.BOOK_PRICE, bookListEntry.bookPrice);
  30.                 contentValues.put(BookOpenHelper.BOOK_AUTHOR, bookListEntry.bookAuthor);
  31.                 bookProvider.insert(BookProvider.CONTENT_URI, contentValues);
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement