使用 space 和 OR 从 java 调用 Twitter RestAPI

Twitter RestAPI call from java using space and OR

在此 link 中,已经解释了如何使用 Twitter 搜索 API,例如: 您的搜索 URL 是:

https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent

当我使用来自 POSTMAN 的 link 执行 GET 调用(设置 OAuth 1 参数)时,它工作正常,但是当我从 JAVA 执行相同的 GET 调用时,它给了我以下信息错误:

Caused by: java.net.URISyntaxException: Illegal character in query at index 57: https://api.twitter.com/1.1/search/tweets.json?q=superman OR spiderman&result_type=recent
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)

如果我只搜索一个没有 space 和 OR 的单词,效果很好。

听起来您需要正确编码 URL。例如 space 字符必须替换为 %20。这里有一个table of encodings. Really, you should use a built in function. See this thread.