我想使用Kotlin和Retrofit连接到Skyscanner API。 https://rapidapi.com/skyscanner/api/skyscanner-flight-search
尝试进行“创建会话”调用时,出现500错误,但日志未给出具体原因。我只能认为我的帖子数据格式不正确,但是我正在将Retrofit与GSon配合使用来解决这个问题。
一个线索是,在他们的Java示例代码中,他们以以下格式传递表单数据:“ inboundDate = 2019-09-10&children = 0&adults = 1” ,而在从我的GSon转换之后sessionObject类,我的数据格式为 {“ adults”:1,“ country”:“ GB”,“ outboundDate”:“ 2020-01-06”} -我不确定如何,使用Retrofit,我可以采用这种格式传递数据,以及是否是造成500的问题。
以下是一些代码段:
// my object for posting data
class SessionBody {
@SerializedName("country")
var country: String = ""
@SerializedName("currency")
var currency: String = ""
...etc...
// my interface
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("pricing/v1.0/")
fun postUser(@Body sessionBody: SessionBody): Call<Void>
// my connector class
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.addInterceptor(Interceptor { chain ->
val original = chain.request()
val request = original.newBuilder()
.header("X-RapidAPI-Host", "skyscanner-skyscanner-flight-search- v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "...my key here...")
.method(original.method(), original.body())
.build()
return@Interceptor chain.proceed(request)
})
.addInterceptor(interceptor)
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.client(client)
.build()
val api = retrofit.create(TravelEzyApi::class.java)
val call = api.postUser(sessionBody)
这是日志的输出...
D/OkHttp: --> POST https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0/
D/OkHttp: Content-Type: application/x-www-form-urlencoded
Content-Length: 142
X-RapidAPI-Host: skyscanner-skyscanner-flight-search-v1.p.rapidapi.com
X-RapidAPI-Key: ... my key here ...
{"adults":1,"country":"GB","currency":"GBP","destinationPlace":"BKK-sky","locale":"en-GB","originPlace":"LHR-sky","outboundDate":"2020-01-06"}
--> END POST (142-byte body)
D/OkHttp: <-- 500 Internal Server Error
D/OkHttp: Cache-Control: private
Content-Type: application/json
Date: Tue, 22 Oct 2019 10:48:44 GMT
Server: RapidAPI-1.0.32
X-RapidAPI-Region: AWS - eu-west-1
X-RapidAPI-Version: 1.0.32
Content-Length: 2
Connection: keep-alive
{}
D/OkHttp: <-- END HTTP (2-byte body)
任何帮助或线索都将不胜感激。