如何从 Amadeus API (Kotlin Android) 获得 JSON 结果

How do I get the JSON result from Amadeus API (Kotlin Android)

我正在尝试通过以下代码使用 Amadeus Offers Search API

when (val flightOffers = amadeus.shopping.flightOffersSearch.get(
        originLocationCode = "MDZ",
        destinationLocationCode = "MAD",
        departureDate = LocalDate.parse("2020-11-11").toString(),
        adults = 2,
        max = 1
    )) {
        is ApiResult.Success -> {
            if (flightOffers.succeeded) {
                println("RESULT SUCCEEDED")
                println(flightOffers.data)
            }
            else
            {
                println("RESULT DIDN'T SUCCEEDED")
            }
        }
        is ApiResult.Error -> {
            println("RESULT ERROR")
        }
    }

如果我编译 logcat 输出如下:

I/System.out: RESULT SUCCEEDED

这让我觉得 flightOffers.data 是空的。

但是,如果我尝试此代码:

val flightOffers = amadeus.shopping.flightOffersSearch.get(
        originLocationCode = "MDZ",
        destinationLocationCode = "MAD",
        departureDate = LocalDate.parse("2020-11-11").toString(),
        adults = 2,
        max = 1
    )
    println("AMADEUS: $flightOffers")

我得到以下输出:

I/System.out: AMADEUS: Success(meta=Meta(count=1, links={self=https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=MDZ&destinationLocationCode=MAD&departureDate=2020-11-11&adults=2&max=1}), data=[FlightOfferSearch(type=flight-offer, id=1, source=GDS, instantTicketingRequired=false, nonHomogeneous=false, oneWay=false, lastTicketingDate=2020-05-03, numberOfBookableSeats=7, itineraries=[Itinerary(duration=PT18H, segments=[SearchSegment(departure=AirportInfo(iataCode=MDZ, terminal=null, at=2020-11-11T07:10:00), arrival=AirportInfo(iataCode=AEP, terminal=null, at=2020-11-11T08:45:00), carrierCode=AR, number=1403, aircraft=Aircraft(code=738), duration=PT1H35M, id=1, numberOfStops=0, blacklistedInEU=false, co2Emissions=null), SearchSegment(departure=AirportInfo(iataCode=EZE, terminal=A, at=2020-11-11T13:25:00), arrival=AirportInfo(iataCode=MAD, terminal=1, at=2020-11-12T05:10:00), carrierCode=UX, number=42, aircraft=Aircraft(code=789), duration=PT11H45M, id=2, numberOfStops=0, blacklistedInEU=false, co2Emissions=null)])], price=SearchPrice(currency=EUR, total=1151.26, base=510.0, fees=[Fee(amount=0.0, type=SUPPLIER), Fee(amount=0.0, type=TICKETING)], grandTotal=1151.26), pricingOptions=PricingOptions(includedCheckedBagsOnly=true, fareType=[PUBLISHED], corporateCodes=null, refundableFare=false, noRestrictionFare=false, noPenaltyFare=false), validatingAirlineCodes=[UX], travelerPricings=[TravelerPricing(travelerId=1, fareOption=STANDARD, travelerType=ADULT, price=SearchPrice(currency=EUR, total=575.63, base=255.0, fees=null, grandTotal=0.0), fareDetailsBySegment=[FareDetailsBySegment(segmentId=1, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Q, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null)), FareDetailsBySegment(segmentId=2, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Z, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null))]), TravelerPricing(travelerId=2, fareOption=STANDARD, travelerType=ADULT, price=SearchPrice(currency=EUR, total=575.63, base=255.0, fees=null, grandTotal=0.0), fareDetailsBySegment=[FareDetailsBySegment(segmentId=1, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Q, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null)), FareDetailsBySegment(segmentId=2, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Z, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null))])])], dictionaries={locations={MAD={cityCode=MAD, countryCode=ES}, EZE={cityCode=BUE, countryCode=AR}, MDZ={cityCode=MDZ, countryCode=AR}, AEP={cityCode=BUE, countryCode=AR}}, aircraft={789=BOEING 787-9, 738=BOEING 737-800}, currencies={EUR=EURO}, carriers={AR=AEROLINEAS ARGENTINAS, UX=AIR EUROPA}})

这意味着 API 正在返回一个 JSON 但是我不能将 flightOffers 与 gson 一起使用将此数据传递给 DataClass 因为 flightOffers 是一个 ApiResult> 而我不这样做知道如何使用它。根据他们的 library docs ,应该像我第一次尝试那样做。

我很感激能得到的所有帮助和建议。这是我的第一个 Android 应用程序。

很高兴看到社区中有一位新的 Android 开发人员!

所以首先,在Android中你应该避免使用println,而应该使用Log.d/e/w/i,这个方法会在android logcat中打印你的结果。

据我所知,您已成功设置项目并且可以从 sdk 进行查询。

在 android sdk 中,每个 get() 都会给你一个正确的数据对象,而不仅仅是 JSON。您不必负责解析答案。您 flightOffers.data 中的东西实际上是一个 List<FlightOfferSearch>,您可以立即使用!