1

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:

enter image description here

This is the response body (Chinese here should not affect understanding):

enter image description here

This includes raw response (using com.google.gson.JsonObject):

enter image description here

Raw response using com.alibaba.fastjson.JSONObject enter image description here

if change JSONObject to String, it just return the String version of error :(: enter image description here

kate
  • 79
  • 1
  • 9
  • Please include response json body – awesoon Apr 30 '19 at 04:56
  • @awesoon added, thanks – kate Apr 30 '19 at 05:11
  • The keys in the table do not match keys in the `response.body` on the screenshot. Anyway I asked about actual json you have received, not a table. Please include actual json. – awesoon Apr 30 '19 at 05:16
  • @awesoon i cannot received any json, it always report the error shown as the image above, or empty json if using com.google.gson.JsonObject – kate Apr 30 '19 at 05:19
  • You received something (maybe this is valid json, maybe not), inspect the response object and include response raw body. – awesoon Apr 30 '19 at 05:21
  • @awesoon I included raw response... the response body is empty if using com.google.gson.JsonObject, and raw response told me code is 200 but nothing... – kate Apr 30 '19 at 05:29
  • The last screenshot does not produce error you've mentioned in the question (compare `response.body` - on the first screenshot it contains 4 elements, on the second it is empty). Make sure your request fails with JSON parse error and include raw response for that request. – awesoon Apr 30 '19 at 05:38
  • @awesoon raw responses are actually same, no matter which JSONObject type used.... The difference is json parse error or empty json – kate Apr 30 '19 at 05:47
  • You're right, I was confused by `return_code` and `timestamp` – awesoon Apr 30 '19 at 05:51
  • Can you change the Retrofit API to request a String instead of a JSONObject? Then show us the string you received from the server. – JensV Apr 30 '19 at 05:51
  • @JensV changed and posted the response.... – kate Apr 30 '19 at 05:58

1 Answers1

0

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.

JensV
  • 3,997
  • 2
  • 19
  • 43