JavaURI。符合谷歌图书要求的建设者
我正在尝试使用Uri。生成器根据Google Books Api的请求创建兼容的Url。(而不是串联字符串) 这里是我需要创建的url:
GET https://www.googleapis.com/books/v1/volumes?q=green+flowers+inauthor:brown&key=yourAPIKey
第一部分是传递包含空格的搜索查询:
green+flowers
在此之后,我需要添加一个特殊的请求:特殊的关键字是“inuthor”。如您所见,第一个和第二个查询必须使用加号“+”连接起来。我还需要使用以下命令连接到我的第二个查询“brown”:”
但是,我的代码:
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
生成此结果(pimary查询“harry potter”和次查询“rowling”):
https://www.googleapis.com/books/v1/volumes?q=Harry%20potter&inauthor=rowling&maxResults=10&orderBy=relevance&printType=books
我试图使用AppendEncodedPath函数,但它也会产生一个不需要的“?”到字符串的末尾
# 1 楼答案
我找到了一个简单的解决方法:拦截uri。在创建url和应用我的需求之前:
请注意:google books搜索现在正在按预期工作,我使用了以下代码:
防止关键字之间出现两个或三个空格。 希望这能帮助其他人