使用Expedia API獲取航班信息

因爲做project需要,得獲取航班信息。

本來是想試試skyscanenr,然而申請API等了一週沒有結果……( ▼-▼ )


有一個小點想提一下的是,在Expedia的API主頁上,給的請求網址是

http://terminal2.expedia.com/x/mflights/search?

但實際操作的時候要換成這個請求網址:

http://terminal2.expedia.com:80/x/mflights/search?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JEditorPane;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.csvreader.CsvWriter;

public class Main {
	public static void main(String[] args) throws JSONException {
		try {

			LinkedList<Flight> wholeList = new LinkedList<Flight>();
			String[][] CityAirport = { { "Madison", "MSN" },
					{ "San Francisco", "SFO" }, { "New York", "NYC" },
					{ "Chicago", "ORD" }, { "Seattle", "SEA" },
					{ "Boston", "BOS" }, { "Washington D.C.", "DCA" },
					{ "Baltimore", "BWI" }, { "Philadelphia", "PHL" },
					{ "Miami", "MIA" }, { "Minneapolis", "MSP" } };

			int cityNumber = CityAirport.length;
			System.out.println(cityNumber);
			Node[] cityArray = new Node[cityNumber];
			for (int i = 0; i < cityNumber; i++)
				cityArray[i] = new Node(CityAirport[i][0], CityAirport[i][1]);

			String[] dateArray = { "2016-04-24" };

			for (int d = 0; d < dateArray.length; d++) {
				String date = dateArray[d];
				for (int i = 0; i < cityNumber; i++) {
					for (int j = 0; j < cityNumber; j++) {
						if (i == j)
							continue;
						LinkedList<Flight> list = findFlights(cityArray[i],
								cityArray[j], date, 3);
						System.out.println(list);
						wholeList.addAll(list);
						System.out.println();
					}
				}
			}

			CsvWriter wr = new CsvWriter("out.csv");
			for (Iterator<Flight> ite = wholeList.iterator(); ite.hasNext();) {
				Flight flight = ite.next();
				String[] temp = { flight.departureNode.cityName,
						flight.departureNode.airport,
						flight.arrivalNode.cityName,
						flight.arrivalNode.airport, flight.FlightDate,
						flight.price + "" };
				wr.writeRecord(temp);
			}
			wr.flush();
			wr.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static LinkedList<Flight> findFlights(Node leave, Node arrive,
			String date, int fligtLimit) throws IOException, JSONException {
		String key = {your key}
		String urlString = "http://terminal2.expedia.com:80/x/mflights/search?departureAirport="
				+ leave.airport
				+ "&arrivalAirport="
				+ arrive.airport
				+ "&departureDate="
				+ date
				+ "&childTravelerAge=2&apikey="
				+ key;

		System.out.println(urlString);
		HashSet<Double> priceSet = new HashSet<Double>();
		LinkedList<Flight> list = new LinkedList<Flight>();

		URL url = new URL(urlString);
		URLConnection connection = url.openConnection();
		BufferedReader in = new BufferedReader(new InputStreamReader(
				connection.getInputStream()));
		String inputLine = in.readLine();

		JSONObject wholeData = new JSONObject(inputLine);
		JSONArray priceArray = wholeData.getJSONArray("offers");
		for (int i = 0; i < priceArray.length(); i++) {
			JSONObject temp = priceArray.getJSONObject(i);
			double price = temp.getDouble("baseFare");
			if (priceSet.contains(price))
				continue;
			priceSet.add(price);
			list.add(new Flight(leave, arrive, date, price));
			if (priceSet.size() >= fligtLimit)
				break;
		}

		return list;
	}
}

class Node {
	String cityName;
	String airport;

	Node(String cityName, String airport) {
		this.cityName = cityName;
		this.airport = airport;
	}
}

class Flight {
	Node departureNode;
	Node arrivalNode;
	String FlightDate;
	double price;

	public Flight(Node departureNode, Node arrivalNode, String FlightDate,
			double price) {
		this.departureNode = departureNode;
		this.arrivalNode = arrivalNode;
		this.FlightDate = FlightDate;
		this.price = price;
	}

	public String toString() {
		return departureNode.cityName + " to " + arrivalNode.cityName
				+ "  on  " + FlightDate + ": $" + price;
	}
}


發佈了288 篇原創文章 · 獲贊 12 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章