我想从goeuro api获得结果json

时间:2017-04-16 05:31:56

标签: java json node.js api

我正在开发java中的API,以便从GoEuro API获取github的json。我想知道参数我需要用POST方法发送并获得结果作为json并结合我的自定义逻辑。

这是我调用api的java代码。

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不满意。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

首先,网址必须只有http://www.goeuro.com/GoEuroAPI/rest/api/v3/search

然后你需要在请求中使用params添加参数。

您的if response!= 200必须在您提出请求后。

public static void main(String[] args) {
        try {

            URL url = new URL("http://www.goeuro.com/GoEuroAPI/rest/api/v3/search);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            con.setRequestMethod("POST");
            con.setRequestProperty("Accept", "application/json");

            String urlParameters = "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=";

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);
            System.out.println("Post parameters : " + urlParameters);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString());

    }

答案 1 :(得分:0)

但是,我从github问题找到了答案。

这里是必需的参数和预期的json格式。

{
   "arrivalPosition":{
      "positionId":380244
},
   "currency":"EUR",
   "departurePosition":{
      "positionId":380553
   },
   "domain":"com",
   "inboundDate":"2017-04-19T00:00:00.000",
   "locale":"en",
   "outboundDate":"2017-04-18T00:00:00.000",
   "passengers":[
      {
         "age":18,
         "rebates":[]
      }
   ],
   "resultFormat":{
      "splitRoundTrip":true
   },
   "searchModes":[
      "directbus"   
]
}