gpt4 book ai didi

java - Httpclient-4.5.2.jar setEntity 显示 "cannot find symbol"(JAVA - netbeans 8)

转载 作者:行者123 更新时间:2023-11-30 07:16:24 33 4
gpt4 key购买 nike

我正在尝试使用从网站获得的一些代码,该网站通过 API ( http://developer.fantasydata.com ) 公开提供体育数据。

该站点提供了一些示例 JAVA 代码来发出 http 请求。由于某种原因,声明的请求(request)的 setEntity 方法显示“找不到符号错误”。

    package epl.fixtures.test.app;

import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class EPLFixturesTestApp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

HttpClient httpclient = HttpClients.createDefault();

try
{
URIBuilder builder = new URIBuilder("https://api.fantasydata.net/soccer/v2/json/CompetitionDetails/EPL");


URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.setHeader("Ocp-Apim-Subscription-Key", "****************");


// Request body
StringEntity reqEntity = new StringEntity("{body}");
request.setEntity(reqEntity);

HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();

if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

}

}

导致问题的行是 request.setEntity(reqEntity); 行

谁能给我解释一下吗?我已将 apache 中的所有相关 jar 文件添加到项目库目录中。

谢谢

最佳答案

HttpGet 没有 setEntity 方法。这是有道理的,因为请求正文在 GET 请求中没有任何意义。

仅实现 HttpEntityEnclosingRequest 的类有这个方法。

我不知道为什么文档使用它,但是当省略这两行时它似乎可以工作(无论如何看起来毫无意义)。代码:

URIBuilder builder = new URIBuilder("https://api.fantasydata.net/soccer/v2/json/CompetitionDetails/EPL");
URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.setHeader("Ocp-Apim-Subscription-Key", "****************");
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();

if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}

关于java - Httpclient-4.5.2.jar setEntity 显示 "cannot find symbol"(JAVA - netbeans 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38330777/

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