使用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 wholeList = new LinkedList();
			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 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 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 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 priceSet = new HashSet();
		LinkedList list = new LinkedList();

		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;
	}
}


你可能感兴趣的:(4Fun)