2017-08-26 151 views
1

我有一定的捲曲請求 -需要幫助轉換成Java請求捲曲請求

curl -POST -H 'access-key: <apikey>' -H "Content-type: application/json" -d '{ 
"item": "electricity", 
"region": "india", 
"unit": "kWh", 
"quantity": 1.564}' 'https://www.carbonhub.xyz/v1/emissions'` 

我試圖使爲同一個Java對方,這就是我來了到現在 -

package org.kodejava.example.httpclient;  
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.util.EntityUtils; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 

public class getEmissions { 
    public static void main(String[] args) { 
     HttpClient client = HttpClientBuilder.create().build(); 
     HttpPost post = new HttpPost("https://www.carbonhub.xyz/v1/emissions"); 

     List<NameValuePair> data = new ArrayList<>(4); 
     data.add(new BasicNameValuePair("item", "electricity")); 
     data.add(new BasicNameValuePair("region", "india")); 
     data.add(new BasicNameValuePair("unit", "kWh")); 
     data.add(new BasicNameValuePair("quantity", 1.564)); 

     try { 
      post.setEntity(new UrlEncodedFormEntity(data)); 
      post.setHeader("Content-Type","application/json"); 
      // use your api key 
      post.setHeader("access-key","<apikey>"); 
      HttpResponse response = client.execute(post); 

      // Print out the response message 
      System.out.println(EntityUtils.toString(response.getEntity())); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

請讓我知道如何解決這個問題。先謝謝你。

+2

'UrlEncodedFormEntity'是用於編碼內容類型'應用程序/ x-WWW的形式urlencoded'。您需要將'data'轉換爲JSON,最好使用JSON庫。 – Andreas

回答

1

您發送數據的方式POST請求不符合指定的curl命令。

您應該首先爲您的輸入製作一個JSON字符串。有很多庫可以用於此目的,如Jackson,JSON-java,Gson等,或者您可以手動構建JSON字符串(不推薦),然後您應該發送JSON字符串作爲POST請求中的數據。

下面是一個辦法手動構建JSON字符串,然後發送它作爲POST數據 -

package org.kodejava.example.httpclient;  

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.util.EntityUtils; 

import java.io.IOException; 

public class getEmissions { 
    public static void main(String[] args) { 
     HttpClient client = HttpClientBuilder.create().build(); 
     HttpPost post = new HttpPost("https://www.carbonhub.xyz/v1/emissions"); 

     StringBuilder requestData = new StringBuilder("'{"); 
     requestData.append("\"item\"").append(':').append("\"electricity\"").append(','); 
     requestData.append("\"region\"").append(':').append("\"india\"").append(','); 
     requestData.append("\"unit\"").append(':').append("\"kWh\"").append(','); 
     requestData.append("\"quantity\"").append(':').append("1.564"); 
     requestData.append("}'"); 

     StringEntity requestDataEntity = new StringEntity(requestData.toString(),ContentType.APPLICATION_JSON); 
     try { 
      post.setEntity(requestData); 
      // use your api key 
      post.setHeader("access-key","<apikey>"); 
      HttpResponse response = client.execute(post); 

      // Print out the response message 
      System.out.println(EntityUtils.toString(response.getEntity())); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

謝謝,這應該解決問題。 –