Java 调用AlchemyAPI时获取org.apache.http.client.ClientProtocolException
使用apache HttpClient调用Java 调用AlchemyAPI时获取org.apache.http.client.ClientProtocolException,java,apache,apache-httpclient-4.x,alchemyapi,Java,Apache,Apache Httpclient 4.x,Alchemyapi,使用apache HttpClient调用AlchemyAPI获取org.apache.http.client.ClientProtocolException获取textGetTargetDemootation: org.apache.http.ProtocolException:服务器未能使用有效的http响应进行响应 下面是代码片段 URI uri = new URIBuilder() .setScheme("http") .setHost("access.alchemyap
AlchemyAPI
获取org.apache.http.client.ClientProtocolException
获取textGetTargetDemootation
:
org.apache.http.ProtocolException:服务器未能使用有效的http响应进行响应
下面是代码片段
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);
URI=new-URIBuilder()
.setScheme(“http”)
.setHost(“access.alchemyapi.com/calls/text/textgettargetedtouction”)
.setParameter(“apikey”,)
.setParameter(“outputMode”、“json”)
.setParameter(“showSourceText”、“0”)
.setParameter(“目标”,)
.setParameter(“文本”,)
.build();
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(uri);
//添加标题
post.setHeader(“用户代理”);
post.setHeader(“内容类型”、“应用程序/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();
}
}
}
我有一个类似的问题,在我的情况下,这是一个防火墙问题;它关闭连接,因为它理所当然地发现它可疑。尝试关闭防火墙 如果存在异常消息或堆栈跟踪,则应包括该消息(至少在异常源代码行中)@fejese我只能看到原因-
org.apache.http.ProtocolException:服务器无法使用有效的http响应进行响应
感谢您的响应。我尝试在列表中设置属性,而不是用URI设置它。成功了。