@DAO (Data Access Object) – Room

An interface (API) which allows us to interact with our SQLite database tables. This example corresponds to the @Entity and Build A Database entries. Once these have been coded use LiveData to observe the data.

Create a custom class to define your table, using Entity to annotate it

@Dao // Required
public interface ContactDao {

    @Query("SELECT * FROM contacts ORDER BY age") // Database query
    List<ContactEntry> loadAllContacts(); // When loadAllContacts() is  called it will return a List<ContactEntry> containing the results of the query

    @Insert
    void insertContact(ContactEntry contactEntry); // Inserts a new ContactEntry into the table

    @Update(onConflict = OnConflictStrategy.REPLACE) // Set to replace (other options: Abort, Fail, Ignore, Rollback)
    void updateContact(ContactEntry contactEntry); // Update an existing Contact entry

    @Delete
    void deleteContact(ContactEntry contactEntry); // Delete an existing Contact entry

    @Query("SELECT * FROM contacts WHERE name = :name") // We can use the 'name' variable in the query by prefixing it with :
    ContactEntry loadEntryByName(String name); // Returns the ContactEntry which matches the name input by the user
}

To generate the table you will need to Build A Database. To define the table you will need to use @Entity.

Build a URL with Uri Builder includes HttpURLConnection function

URL is a specific class of Uri. Use the following function to compile a valid URL. HttpURLConnection will require this when making a connection.

Method to build URL
    final static String BASE_URL =
            "https://api.github.com/search/repositories"; // Query the GitHub repositories
    final static String PARAM_QUERY = "q";
    final static String PARAM_SORT = "sort";
    final static String sortBy = "stars"; // Sorted by the number of stars the repo has

    public static URL buildUrl(String searchQuery) {
        Uri builtURI = Uri.parse(BASE_URL)
                .buildUpon()
                .appendQueryParameter(PARAM_QUERY, searchQuery)
                .appendQueryParameter(PARAM_SORT, sortBy)
                .build();
        try {
            URL url = new URL(builtURI.toString());
            Log.d("buildUrl produced URL ", url.toString()); // Produces URL: https://api.github.com/search/repositories?q=android&sort=stars
            return url;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            Log.d("buildUrl", "Exception thrown");
        }
        return null;
    }

A typical helper method for fetching the results of the query based on the URL.

    public static String getResponseFromHttpUrl(URL url) throws IOException {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            InputStream in = urlConnection.getInputStream();

            Scanner scanner = new Scanner(in);
            scanner.useDelimiter("\\A"); // \A represents the beginning of the stream. Forces the scanner to read the entire contents of the stream
// It buffers the data and also converts the stream from UTF-8 (JSON/JS) to UTF-16 (which Android uses)
            boolean hasInput = scanner.hasNext();
            if (hasInput) {
                return scanner.next();
            } else {
                return null;
            }
        } finally {
            urlConnection.disconnect();
        }
    }

ud851-Exercises-student\Lesson02-GitHub-Repo-Search\T02.06-Exercise-AddPolish