- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Uri.Builder 按照 Google Books API 的请求创建兼容的网址。 (而不是连接字符串)这是我需要创建的 URL:
获取 https://www.googleapis.com/books/v1/volumes?q=green+flowers+inauthor:brown&key=yourAPIKey
第一部分是传递包含空格的搜索查询:green+flowers
在此之后我需要添加一个特殊请求:特殊关键字是“inauthor”。正如您所看到的,第一个和第二个查询必须使用加号“+”连接起来。我还需要使用这个“:”连接到我的第二个查询“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
产生此结果(主要查询“harry potter”和次要查询“rowling”):
https://www.googleapis.com/books/v1/volumes?q=Harry%20potter&inauthor=rowling&maxResults=10&orderBy=relevance&printType=books
我尝试使用 AppendEncodedPath 函数,但它也产生了一个不需要的“?”到字符串末尾。
最佳答案
我找到了一个简单的解决方法:在创建 url 并应用我的需求之前拦截 uri.builder:
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;
}
请注意:Google 图书搜索现在按预期工作,我使用了以下代码:
url = url.replace("%20%20%20", "+");
url = url.replace("%20%20", "+");
url = url.replace("%20", "+");
防止关键字之间有两个或三个空格。希望这对其他人有帮助。
关于java - Uri.Builder 以满足 Google 图书要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58331524/
前几天收到了由戴路遥编著的《互联网科普书籍-站长之家》,打开书一看把我惊呆了,大家可以先看一下图书封面和目录。 如图所示,如何用百度,如何用淘宝,如何注册域名、如何用网站空间,甚至是如何用QQ
我正在尝试使用 Google 图书 API,但对于任何请求,我都会收到 totalItems = 0。 我尝试在 Chrome 中发送请求,例如: https://www.googleapis.com
我正在通过检索 JSON 数据将一些 Google 图书信息发送到我的数据库。我的代码如下 $.ajax({ type: 'POST', url: 'addIsbnScript.p
有没有办法以编程方式检索用户已购买的 Kindle 电子书列表? 我已经在 SO 上广泛搜索了答案,但是我能找到的几个相关问题都是古老的。亚马逊文档也不是很有帮助,因为据我所知,亚马逊联盟计划 API
我无法在此 Json 响应中提取书籍缩略图的 URL: https://www.googleapis.com/books/v1/volumes?q=java&maxResults=10 当我将链接硬编
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭12 年前。 Improve th
这是我的点击信息: Button searchButton = (Button) findViewById(R.id.search_button); searchButton.
如何在 Swift 中使用 JSON 从谷歌的书中获取作者?此代码有效,但我对作者有疑问。 这是一个 JSON 的例子: { "kind": "books#volume", "id": "17B
本文节选自清华大学出版社出版的图书《数据资产管理核心技术与应用》,作者为张永清等著。 从Spark 执行计划中获取数据血缘 因为数据处理任务会涉及到数据的转换和处理,所以从数据任务中解析血缘也是获
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 3年前关闭。 Improve this qu
借助 Google Book Search API V1,我可以搜索图书。 现在,每当用户尝试使用不同的关键字进行搜索时,我只想搜索特定类别的书籍。 我无法在 Book API V1 中设置类别 ht
我想弄清楚如何使用 Google Books API 按 ISBN 搜索图书。我需要编写一个程序来搜索 ISBN,然后打印出标题、作者和版本。我尝试使用 List volumesList = book
我是一名优秀的程序员,十分优秀!