gpt4 book ai didi

java - 为什么java导入org.apache.http包后找不到StringEntity类?

转载 作者:行者123 更新时间:2023-12-01 20:52:10 25 4
gpt4 key购买 nike

我正在使用 Microsoft 认知服务中的示例代码。它利用 StringEntity 类向 REST API 发出请求。 java编译器似乎无法找到'symbol' StringEntity?这可能是什么原因以及如何解决?这是代码 -

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.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.*;


public class NEWS_FETCHER
{
public static void main(String[] args)
{
HttpClient httpclient = HttpClients.createDefault();

try
{
URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/news/search");

builder.setParameter("q", "microsoft");
builder.setParameter("count", "10");
builder.setParameter("offset", "0");
builder.setParameter("mkt", "en-us");
builder.setParameter("safeSearch", "Moderate");

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

// Request body
StringEntity reqEntity = new StringEntity("");
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());
}
}
}

大部分内容取自 Microsoft API 测试示例。 https://dev.cognitive.microsoft.com/docs/services/56b43f72cf5ff8098cef380a/operations/56b449fbcf5ff81038d15cdf

最佳答案

StringEntity位于包org.apache.http.entity中,因此您必须为其添加正确的导入:

import org.apache.http.entity.StringEntity

关于java - 为什么java导入org.apache.http包后找不到StringEntity类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165291/

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