Reputation: 79
I am trying to use RoyalPay SDK to create order and make Alipay payment. The response code is 200, but I cannot parse JSON in response.
How do I solve this problem?
The code I create api request with:
interface RoyalPayApi {
@FormUrlEncoded
@Headers("Accept: application/json", "Content-Type: application/json")
@PUT("/api/v1.0/gateway/partners/{partner_code}/app_orders/{order_id}")
fun createRoyalPaySDKOrder(@Path(value = "partner_code", encoded = true) partner_code: String, @Path(value = "order_id", encoded = true) order_id: String,
@Query("time") time: Long, @Query("nonce_str") nonce_str: String, @Query("sign") sign: String,
@Field("description") description: String,
@Field("price") price: Int,
@Field("currency") currency: String,
@Field("channel") channel: String,
@Field("operator") operator: String,
@Field("system") system: String): Call<JSONObject> // com.alibaba.fastjson.JSONObject
}
The code I get retrofit service:
fun createService(): RoyalPayApi {
val retrofit = Retrofit.Builder()
.baseUrl(ROYAL_PAY_ADDRESS)
.addConverterFactory(FastJsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
return retrofit.create(RoyalPayApi::class.java)
}
The code I send request and receive response:
var api = createService()
var call = api.createRoyalPaySDKOrder(ROYAL_PAY_PARTNER_CODE, order_id, time, ROYAL_PAY_NONCE_STR, sign,
description, price, "AUD", channel, "kate", "android")
call.enqueue(object : Callback<JSONObject>{
override fun onResponse(call: Call<JSONObject>, response: Response<JSONObject>) {
val str = ""
}
override fun onFailure(call: Call<JSONObject>, t: Throwable) {
val str = ""
}
})
This is the response I received:
This is the response body (Chinese here should not affect understanding):
This includes raw response (using com.google.gson.JsonObject):
Raw response using com.alibaba.fastjson.JSONObject
if change JSONObject to String, it just return the String version of error :(:
Upvotes: 1
Views: 3514
Reputation: 4544
The server is expecting a JSON Body request. You are annotating your data with @Field
which will result in the request formed as a queryString.
I.e. your request body will look something like this:
description=foo&price=123...
instead of this:
{
"description": "foo",
"price": 123,
...
}
To achieve what you want, check this question here. The first answer works directly with Java objects, but you can also use the second answer if you don't want to work with custom classes.
Upvotes: 0