我正在尝试使用从网站获得的一些代码,该网站具有通过API(http://developer.fantasydata.com)公开提供的体育数据。
该站点提供了一些示例JAVA代码来发出http请求。由于某种原因,已声明的请求(请求)的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));
}