gpt4 book ai didi

java - 调用 AlchemyAPI 时获取 org.apache.http.client.ClientProtocolException

转载 作者:太空宇宙 更新时间:2023-11-04 06:19:34 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com