我正在開發java中的API來獲取json從GoEuro API可在github處獲得。我想知道我需要用POST方法發送的參數,並將結果作爲json並與我的自定義邏輯結合使用。我想從goeuro api得到結果json
這是我的java代碼來調用api。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JsonEncodeDemo {
public static void main(String[] args) {
try {
URL url = new URL("http://www.goeuro.com/GoEuroAPI/rest/api/v3/search?departure_fk=318781&departure_date=16%2F04%2F2017&arrival_fk=380553&trip_type=one-way&adults=1&children=0&infants=0&from_filter=Coventry+%28COV%29%2C+United+Kingdom&to_filter=London%2C+United+Kingdom&travel_mode=bus&ab_test_enabled=");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()), "UTF-8"));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我不擅長nodejs。任何幫助,將不勝感激。
您是否收到任何錯誤?你能解釋一下你的預期產出嗎? –
首先我不確定,我附加了所需的參數。輸出也應該在json字符串中。 –
你得到這個錯誤的迴應? :{ 「message」:「400:Bad Request」, 「errorClass」:「InvalidRequestEntityException」, 「description」:「Validation failed」, 「items」:[ 「SearchQuery.locale may not be null」, 「SearchQuery.domain不能爲空」, 「SearchQuery.departurePosition不能爲空」, 「SearchQuery.outboundDate不能爲空」, 「SearchQuery.resultFormat不能爲空」, 「SearchQuery.arrivalPosition可能不爲空「, 」SearchQuery.currency可能不爲空「 ] } –