Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Google QPX API with Java for flight search?

I am trying to build a simple Java code that obtains flight options using Google QPX API, for the flight from New York to London. I signed up with Google and got API_key.

I read the documentation, but unfortunately, I couldn't find any example that show me how to do it.

Here what I tried so far:

import com.google.api.services.qpxExpress.model.*;
import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {   
        // Passengers
        PassengerCounts passengers = new PassengerCounts();
        passengers.setAdultCount(2);

        // Slice
        List<SliceInput> slices = new ArrayList<SliceInput>();

        SliceInput slice = new SliceInput();
        slice.setOrigin("JFK"); // John Kennedy Airport in Ney York
        slice.setDestination("LHR"); // London Heathrow
        slice.setDate("2015-07-01");
        slices.add(slice);


        // Options request
        TripOptionsRequest tripOptions = new TripOptionsRequest(); 
        tripOptions.setPassengers(passengers);
        tripOptions.setSlice(slices);


        // Search request
        TripsSearchRequest tripSearchReq = new TripsSearchRequest();
        tripSearchReq.setRequest(tripOptions);

        // Next steps?
        // Setting up QPXExpress?

    }
}

I will appreciate it if someone can help me to complete the code. Thanks in advance.

like image 810
Akram Avatar asked Feb 27 '15 03:02

Akram


People also ask

Is there an API for Google Flights?

What is the Google Flights API? Google Flights is a solution Google provides that allows you to search “cheap flights in seconds, explore destinations on a map, and sign up for fare alerts.” Their API (also known as QPX Express) allows developers to access this data to search and compare airline flight prices.

How do I get data from Google Flights?

Track prices for a route or flightGo to Google Flights. At the top, choose the number of stops, cabin class, and how many tickets you need. Choose your departure airport and destination. To track prices for this route, turn on Track prices.

What is Google Qpx?

QPX. Flight search and pricing engine behind Google Flight Search (GFS) and also behind many major airline websites.


1 Answers

This is a simple code, successfully tested, hope it helps

package com.airline.api;

import java.io.IOException;
import java.util.*;

import javax.swing.text.View;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.qpxExpress.QPXExpressRequestInitializer;
import com.google.api.services.qpxExpress.QPXExpress;
import com.google.api.services.qpxExpress.model.FlightInfo;
import com.google.api.services.qpxExpress.model.LegInfo;
import com.google.api.services.qpxExpress.model.PassengerCounts;
import com.google.api.services.qpxExpress.model.PricingInfo;
import com.google.api.services.qpxExpress.model.SegmentInfo;
import com.google.api.services.qpxExpress.model.SliceInfo;
import com.google.api.services.qpxExpress.model.TripOption;
import com.google.api.services.qpxExpress.model.TripOptionsRequest;
import com.google.api.services.qpxExpress.model.TripsSearchRequest;
import com.google.api.services.qpxExpress.model.SliceInput;
import com.google.api.services.qpxExpress.model.TripsSearchResponse;

public class AirlineReservation {

    /**
     * @param args
     */


      private static final String APPLICATION_NAME = "MyFlightApplication";

      private static final String API_KEY = "AIzaSyDnBCdsmTnrL5XFrO2TjJyvFioswjakNYU";

      /** Global instance of the HTTP transport. */
      private static HttpTransport httpTransport;

      /** Global instance of the JSON factory. */
      private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();




    public static void main(String[] args) {
        // TODO Auto-generated method stub

         try {
              httpTransport = GoogleNetHttpTransport.newTrustedTransport();


              PassengerCounts passengers= new PassengerCounts();
                passengers.setAdultCount(1);

                List<SliceInput> slices = new ArrayList<SliceInput>();

                SliceInput slice = new SliceInput();
                slice.setOrigin("NYC"); 
                slice.setDestination("LGA"); 
                slice.setDate("2015-04-29");
                slices.add(slice);

                TripOptionsRequest request= new TripOptionsRequest();
                request.setSolutions(10);
                request.setPassengers(passengers);
                request.setSlice(slices);

                TripsSearchRequest parameters = new TripsSearchRequest();
                parameters.setRequest(request);
                QPXExpress qpXExpress= new QPXExpress.Builder(httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME)
                        .setGoogleClientRequestInitializer(new QPXExpressRequestInitializer(API_KEY)).build();

                TripsSearchResponse list= qpXExpress.trips().search(parameters).execute();
                List<TripOption> tripResults=list.getTrips().getTripOption();

                String id;

                for(int i=0; i<tripResults.size(); i++){
                    //Trip Option ID
                    id= tripResults.get(i).getId();
                    System.out.println("id "+id);

                    //Slice
                    List<SliceInfo> sliceInfo= tripResults.get(i).getSlice();
                    for(int j=0; j<sliceInfo.size(); j++){
                        int duration= sliceInfo.get(j).getDuration();
                        System.out.println("duration "+duration);
                        List<SegmentInfo> segInfo= sliceInfo.get(j).getSegment();
                        for(int k=0; k<segInfo.size(); k++){
                            String bookingCode= segInfo.get(k).getBookingCode();
                            System.out.println("bookingCode "+bookingCode);
                            FlightInfo flightInfo=segInfo.get(k).getFlight();
                            String flightNum= flightInfo.getNumber();
                            System.out.println("flightNum "+flightNum);
                            String flightCarrier= flightInfo.getCarrier();
                            System.out.println("flightCarrier "+flightCarrier);
                            List<LegInfo> leg=segInfo.get(k).getLeg();
                            for(int l=0; l<leg.size(); l++){
                                String aircraft= leg.get(l).getAircraft();
                                System.out.println("aircraft "+aircraft);
                                String arrivalTime= leg.get(l).getArrivalTime();
                                System.out.println("arrivalTime "+arrivalTime);
                                String departTime=leg.get(l).getDepartureTime();
                                System.out.println("departTime "+departTime);
                                String dest=leg.get(l).getDestination();
                                System.out.println("Destination "+dest);
                                String destTer= leg.get(l).getDestinationTerminal();
                                System.out.println("DestTer "+destTer);
                                String origin=leg.get(l).getOrigin();
                                System.out.println("origun "+origin);
                                String originTer=leg.get(l).getOriginTerminal();
                                System.out.println("OriginTer "+originTer);
                                int durationLeg= leg.get(l).getDuration();
                                System.out.println("durationleg "+durationLeg);
                                int mil= leg.get(l).getMileage();
                                System.out.println("Milleage "+mil);

                            }

                        }
                    }

                    //Pricing
                    List<PricingInfo> priceInfo= tripResults.get(i).getPricing();
                    for(int p=0; p<priceInfo.size(); p++){
                        String price= priceInfo.get(p).getSaleTotal();
                        System.out.println("Price "+price);
                    }

                }
              return;
            } catch (IOException e) {
              System.err.println(e.getMessage());
            } catch (Throwable t) {
              t.printStackTrace();
            }
            System.exit(1);

    }
}
like image 58
Lola Loulita Avatar answered Oct 13 '22 11:10

Lola Loulita