'Uri.Builder to match Google Books requirements

I'm trying to use Uri.Builder to create a compatible URL as requested by Google Books API. (instead of concatenating strings) Here the URL I need to create:

GET https://www.googleapis.com/books/v1/volumes?q=green+flowers+inauthor:brown&key=yourAPIKey

The first part is to pass a search query which contains a space: green+flowers

After this I need to add a special request: the special keyword is "inauthor". As you can see first and second queries must be concatenated by using a plus "+". I also need to concatenate to my second query "brown" using this ":"

However my code:

        Uri.Builder uriBuilder = new Uri.Builder();
        uriBuilder.scheme("https")
                .authority("www.googleapis.com")
                .appendPath("books").appendPath("v1").appendPath("volumes")
                .appendQueryParameter("q", primaryQuery);
        //Search Mode
        switch (sharedPreferences.readSharedPreferencesInt(this.getBaseContext(), "searchMode", 0)) {
            case 0: //Google default
                //doNothing
                break;
            case 1: //byTitle
                uriBuilder.appendQueryParameter("intitle", secondaryQuery);
                break;
            case 2: //byAuthor
                uriBuilder.appendQueryParameter("inauthor", secondaryQuery);
                break;
            case 3: //byPublisher
                uriBuilder.appendQueryParameter("inpublisher", secondaryQuery);
                uriBuilder.
                break;
            case 4://bySubject
                uriBuilder.appendQueryParameter("subject", secondaryQuery);
                break;
            case 5: //byISBN
                uriBuilder.appendQueryParameter("isbn", secondaryQuery);
                break;
            case 6: //byLCCN
                uriBuilder.appendQueryParameter("lccn", secondaryQuery);
                break;
            case 7: //byOCLC
                uriBuilder.appendQueryParameter("oclc", secondaryQuery);
                break;
        }

        uriBuilder.appendQueryParameter("maxResults", String.valueOf((sharedPreferences.readSharedPreferencesInt(this.getBaseContext(), "maxResults", 10) + 10)));//Default for maxResults is 10 but we choose 20

produces this result (primary query "harry potter" and secondary "rowling"):

https://www.googleapis.com/books/v1/volumes?q=Harry%20potter&inauthor=rowling&maxResults=10&orderBy=relevance&printType=books

I tried to use AppendEncodedPath function but it produces also an unwanted "?" to the end of the string.



Solution 1:[1]

I found a simple workaround: intercept the uri.builder prior to create the url and applying my needs:

        String url = uriBuilder.toString();
        url = url.replace("%20%20%20", "+");
        url = url.replace("%20%20", "+");
        url = url.replace("%20", "+");
        url = url.replace("&intitle=", "+intitle:");
        url = url.replace("&inauthor=", "+inauthor:");
        url = url.replace("&inpublisher=", "+inpublisher:");
        url = url.replace("&subject=", "+subject:");
        url = url.replace("&isbn=", "+isbn:");
        url = url.replace("&lccn=", "+lccn:");
        url = url.replace("&oclc=", "+oclc:");

        //return createUrl(uriBuilder.toString());
        return createUrl(url);
    }

    private static URL createUrl(String stringUrl) {
        URL url = null;
        try {
            url = new URL(stringUrl);
        } catch (MalformedURLException e) {
            Log.e(LOG_TAG, "Error creating URL ", e);
        }
        return url;
    }

Please note: the google books search is now working as expected, I used this code:

        url = url.replace("%20%20%20", "+");
        url = url.replace("%20%20", "+");
        url = url.replace("%20", "+");

to prevents two or three spaces between keywords. Hope this should help someone else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Rogers