2017-04-11 16 views
0

我試圖在Java的一個post請求使用Apache HTTP components,當我把我的實體,在這條線構造UrlEncodedFormEntity(名單<的NameValuePair>,字符串)是在建設POST請求未定義的錯誤

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

它說

"The constructor UrlEncodedFormEntity(List, String) is undefined" and I am not sure why.

這裏是我整個代碼

@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(); 
       } 
      } 
    } 

我搜索過的每個資源都顯示這是正確的方式,所以我不確定它爲什麼返回undefined。

回答

0

也許可能存在衝突的Apache HTTP組件JAR文件。 我試過下面的代碼,並沒有編譯錯誤: 這些是classpath中使用的兩個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(); 
     } 
    } 
} 
相關問題