我想使用Kotlin和Retrofit连接到天巡应用程序接口。https://rapidapi.com/skyscanner/api/skyscanner-flight-search
当我尝试POST 'create session'调用时,我得到了一个500错误,但是日志没有给出具体的原因。我只能假设我的post数据没有被正确格式化,但是我正在使用Retrofit with GSon来处理这个问题。
一个线索是,在他们的Java示例代码中,他们以以下格式传递表单数据:“inboundDate=2019-09-10&children=0&adults=1”而在从我的sessionObject类进行GSon转换后,我的数据格式为{“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)
任何帮助或线索,不胜感激。
1条答案
按热度按时间wpx232ag1#
500状态代码表示服务器遇到了不知道如何处理的情况。
如果错误仍然存在,您可以随时写信给RapidAPI支持团队,网址为support@rapidapi.com