- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 Apache HttpClient 为 TextGetTargetedSentiment
调用 AlchemyAPI
时遇到 org.apache.http.client.ClientProtocolException
:
org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
下面是代码片段。
URI uri = new URIBuilder()
.setScheme("http")
.setHost("access.alchemyapi.com/calls/text/TextGetTargetedSentiment")
.setParameter("apikey", <api-key>)
.setParameter("outputMode", "json")
.setParameter("showSourceText", "0")
.setParameter("target", <target>)
.setParameter("text", <news article>)
.build();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uri);
// add header
post.setHeader("User-Agent", <user agent>);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post);
感谢您的帮助。
最佳答案
我怀疑您用于目标和文本的值可能有些奇怪,可能会把事情搞砸。如果此示例未能澄清问题,您应该发送电子邮件至 questions@alchemyapi.com 以获得进一步支持。以下对我有用:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Main {
public static void main(String[] args) {
try
{
HttpClient client = new DefaultHttpClient();
String text = "Text analytics is fun with AlchemyAPI.";
String API_KEY = ""; // Add your API key here
URI uri = new URIBuilder()
.setScheme("http")
.setHost("access.alchemyapi.com/calls/text/TextGetTargetedSentiment")
.setParameter("apikey", API_KEY)
.setParameter("outputMode", "json")
.setParameter("showSourceText", "1")
.setParameter("target", "Text analytics")
.setParameter("text", text)
.build();
HttpPost post = new HttpPost(uri);
//the following headers aren't necessary for getting a response
post.setHeader("User-Agent", "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
System.out.println( "executing request " + post.getRequestLine());
HttpResponse response = client.execute(post);
String xmlString = EntityUtils.toString(response.getEntity());
System.out.println(xmlString);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
关于java - 调用 AlchemyAPI 时获取 org.apache.http.client.ClientProtocolException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592293/
我正在尝试 AlchemyAPI_Python-0.6 - PyXml 模块。我试图运行它的关键字提取器功能,但在尝试编译时出现以下错误。我使用了示例中给出的 keywords.py 文件。我复制了
在使用 Alchamy 的实体 API 时收到“截断过大文本内容”的警告 - 有谁知道字符限制 - 我在他们的支持页面上找不到。 最佳答案 文本限制为50kb,在this page的底部有点隐藏. 关
代码的 github 链接如下: https://github.com/AlchemyAPI/alchemyapi-recipes-twitter 运行recipe.py时出现以下错误: Traceb
我想在我的 Java 项目中使用 AlchemyAPI。我正在使用 Maven 进行依赖管理。我打算使用 this Maven 存储库。所以,我想知道是否有任何文档可用。另外,我想知道如何使用我的 A
AlchemyAPI 的免费用户每天可以调用 1000 个请求 (http://www.alchemyapi.com/products/pricing/)。 我一直在使用 python 访问 API:
我正在编写使用 Ruby 开始使用 AlchemyAPI Ruby 教程。但我似乎找不到我的 API key 。 来源:http://www.alchemyapi.com/developers/get
我真的很喜欢使用 Alchemy API REST api,并且尝试过 php sdk,但我正在尝试编译 API,但当我尝试进行 make 时,却不断遇到非常奇怪的问题。 这是尝试使用 Bitnami
正在尝试安装 AlchemyAPI.py-2.5 无法运行示例。 Debian 6.0 python 示例/sentiment.py: Traceback (most recent call last
使用 Apache HttpClient 为 TextGetTargetedSentiment 调用 AlchemyAPI 时遇到 org.apache.http.client.ClientProto
我是一名优秀的程序员,十分优秀!