【问题标题】:The constructor UrlEncodedFormEntity(List<NameValuePair>, String) is undefined error in building post request构造函数 UrlEncodedFormEntity(List<NameValuePair>, String) 在构建发布请求时出现未定义错误
【发布时间】:2017-04-11 05:15:19
【问题描述】:

当我在这一行设置我的实体时,我正在尝试使用Apache HTTP components 在 java 中发出 post 请求

httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

上面写着

“构造函数 UrlEncodedFormEntity(List, String) 未定义”,我不知道为什么。

这是我的全部代码

@Component
public class ScheduledTasks {

@Scheduled(cron="0 9 1-7 * 1 *")    //first monday of each month, at 9am
public void dataLoaderTask() throws Exception   {
    HttpClient httpclient = HttpClients.createDefault();
    HttpPost httppost = new HttpPost("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search");
        List<NameValuePair> params = new ArrayList<NameValuePair>(3);
            params.add(new NameValuePair("action", "count"));
            params.add(new NameValuePair("fields", "Status"));
            params.add(new NameValuePair("filters", ""));
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            //Execute and get the response.
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    // do something useful
                } finally {
                    instream.close();
                }
            }
    }

我搜索过的每个资源都表明这是这样做的正确方法,所以我不确定它为什么返回未定义。

【问题讨论】:

    标签: java apache http post


    【解决方案1】:

    可能存在冲突的 Apache HTTP 组件 JAR 文件。 我尝试了下面的代码,没有编译错误: 这些是类路径中使用的两个 JAR 文件:http-client-4.5.3.jar、http-core-4.4.6.jar。使用 spring-boot 将确保您的存储库中的开源 JAR 文件是最新版本。

    @Scheduled(cron = "0 9 1-7 * 1 *") // first monday of each month, at 9am
    public void dataLoaderTask() throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("https://erudite-master-api-awsmaui.lab.expts.net/erudite/search");
        List<NameValuePair> params = new ArrayList<NameValuePair>(3);
        params.add(new BasicNameValuePair("action", "count"));
        params.add(new BasicNameValuePair("fields", "Status"));
        params.add(new BasicNameValuePair("filters", ""));
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        // Execute and get the response.
        CloseableHttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                // do something useful
            } finally {
                instream.close();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多