public Repo readMessage(JsonReader reader) throws IOException { repo = new Repo(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { repo.setName(reader.nextString()); } else if (name.equals("description")) { repo.setDescription(reader.nextString()); } else if (name.equals("owner")) { ReadOwnerMessage(reader); } else if (name.equals("html_url")) { repo.setRepo_url(reader.nextString()); } else if (name.equals("fork")) { repo.setFork(reader.nextBoolean()); } else { reader.skipValue(); } } reader.endObject(); return repo; }
public void ReadOwnerMessage(JsonReader reader) throws IOException { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("login")) { repo.setOwner_name(reader.nextString()); } else if (name.equals("html_url")) { repo.setOwner_url(reader.nextString()); } else { reader.skipValue(); } } reader.endObject(); }
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // if the cursor is empty... get data from the internet if (data.getCount() > 1) { // moving cursor to first data.moveToFirst(); do { Repo repo = new Repo(); repo.setName(data.getString(COL_NAME)); repo.setOwner_name(data.getString(COL_OWNER_NAME)); repo.setOwner_url(data.getString(COL_OWNER_URL)); repo.setDescription(data.getString(COL_DESC)); repo.setRepo_url(data.getString(COL_REPO_URL)); repo.setFork(data.getInt(COL_FORK) == 0); reposArrayList.add(repo); } while (data.moveToNext()); mAdapter.notifyDataSetChanged(); } else { // show progress form showProgress(true); // get some data new GithubApiFetcher().execute("1"); } }
/** * Use this method to add data to the content provider * * @param reposArrayList list of {@link Repo} */ private void cacheData(ArrayList<Repo> reposArrayList) { ContentValues contentValues; for (Repo repo : reposArrayList) { contentValues = new ContentValues(); contentValues.put(RepoEntry.COLUMN_NAME, repo.getName()); contentValues.put(RepoEntry.COLUMN_OWNER_NAME, repo.getOwner_name()); contentValues.put(RepoEntry.COLUMN_DESCRIPTION, repo.getDescription()); contentValues.put(RepoEntry.COLUMN_OWNER_URL, repo.getOwner_url()); contentValues.put(RepoEntry.COLUMN_REPO_URL, repo.getRepo_url()); contentValues.put(RepoEntry.COLUMN_FORK, repo.isFork() ? 0 : 1); // insert the content value to the content provider getActivity().getContentResolver().insert(RepoEntry.CONTENT_URI, contentValues); } }