首页 > 解决方案 > 如何从 Amadeus API (Kotlin Android) 获取 JSON 结果

问题描述

我正在尝试通过以下代码使用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,但是我不能使用带有 gson 的 flightOffers 将此数据传递给 DataClass,因为 flightOffers 是 ApiResult> 并且我不知道如何使用它。根据他们的图书馆文档,应该像我在第一次尝试时那样做。

我感谢我能得到的所有帮助和建议。这是我的第一个安卓应用。

标签: androidjsonapikotlinamadeus

解决方案


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

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

对于我所看到的,您成功设置了您的项目,并且能够从 sdk 进行查询。

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


推荐阅读