Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onHandleIntent(Intent intent) {
- Log.d(this.getClass().getName(), "New intent received.");
- BookProvider bookProvider = new BookProvider();
- bookProvider.setContext(this);
- String url = this.getResources().getString(R.string.api_url) + "books";
- InputStream source = retrieveStream(url);
- StringWriter writer = new StringWriter();
- String retrievedJSON = "";
- if (source != null) {
- try {
- IOUtils.copy(source, writer);
- retrievedJSON = writer.toString();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (!retrievedJSON.equals("")) {
- Gson gson = new Gson();
- BookListEntry[] bookListEntries = gson.fromJson(retrievedJSON, BookListEntry[].class);
- for (BookListEntry bookListEntry : bookListEntries) {
- ContentValues contentValues = new ContentValues();
- contentValues.put(BookOpenHelper.BOOK_ID, bookListEntry.bookId);
- contentValues.put(BookOpenHelper.BOOK_TITLE, bookListEntry.bookTitle);
- contentValues.put(BookOpenHelper.BOOK_ISBN, bookListEntry.bookISBN);
- contentValues.put(BookOpenHelper.BOOK_PRICE, bookListEntry.bookPrice);
- contentValues.put(BookOpenHelper.BOOK_AUTHOR, bookListEntry.bookAuthor);
- bookProvider.insert(BookProvider.CONTENT_URI, contentValues);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement