skip to Main Content

I don’t understand what is problem clearly. When I searched it in google, I don’t decide my reponse model is problem or the json response is problem and should change. Which one? I can’t find solution for Kotlin. How I should solve this?

response JSON:

"data":{
      "productInfo":{
         "data":{
            "toBarcode":"2704439285463",
            "productJson":{
               "p_no":"28420000",
               "p_name":"ASA"
            }
         }
      },
      "moves":{
         "data":[
            {
               "fisAcik":"MALVERENDEN",
               "toBarcode":"2704439285463",
               "toJson":{
                  "to_Hks_Adi":"DAĞITIM MERKEZİ"
               },
               "movementJson":{
                  "isleme_Tarihi":"21/12/2022 02:19:30"
               }
            }
         ]
      }
   }

Data.kt

data class Data(
        val productInfo: ProductInfo,
        val moves: Moves
    )
    
    data class Moves (
        val data: List<MovesItem>
    )
    
    data class MovesItem (
        @SerializedName("fisAcik")
        val receiptExplanation: String,
        val toBarcode: String,
        val toJson: ToJson,
        val movementJson: MovementJson
    )
    
    data class MovementJson (
        @SerializedName("isleme_Tarihi")
        val processDate: String
    )
    
    data class ToJson (
        @SerializedName("to_Hks_Adi")
        val toUnitHksName: String
    )
    
    data class ProductInfo (
        val data: ProductInfoItems
    )
    
    data class ProductInfoItems (
        val toBarcode: String,
        val productJson: ProductJson
    )
    
    data class ProductJson (
        @SerializedName("p_No")
        val migrosProductNo: String,
        @SerializedName("p_Name")
        val migrosProductName: String
    )

method that using to call request.

suspend fun dataGetInfo(@Body request: DataRequest): NetworkResult<BaseResponse<Data>>

2

Answers


  1. Chosen as BEST ANSWER

    This error was from my wrong request. I saw Ios has same error also when request with wrong value. So, for who will look this quesiton, they should understand it's not from response or kotlin. Check your value it is clearly what request need.


  2. The framework you are using for this:
    ...fun dataGetInfo(@Body request: DataRequest)...
    is implicitly taking a JSON request and deserializing.

    The annotation @SerializedName is a from the Gson library, so I guessed that your framework must be using Gson. From that I was able to test using:

    import com.google.gson.Gson
    import com.google.gson.annotations.SerializedName
    
    println(Gson().fromJson(src, Data::class.java))
    

    which produces

    Data(productInfo=ProductInfo(data=ProductInfoItems(toBarcode=2704439285463, productJson=ProductJson(migrosProductNo=null, migrosProductName=null))), moves=Moves(data=[MovesItem(receiptExplanation=MALVERENDEN, toBarcode=2704439285463, toJson=ToJson(toUnitHksName=DAĞITIM MERKEZİ), movementJson=MovementJson(processDate=21/12/2022 02:19:30))]))
    

    So fundamentally your code is ok, but I think the problem is how the source JSON is "topped and tailed". To get that parse work, I was using

        val src = """
            {
              "productInfo": {
                "data": {
                  "toBarcode": "2704439285463",
                  "productJson": {
                    "p_no": "28420000",
                    "p_name": "ASA"
                  }
                }
              },
              "moves": {
                "data": [
                  {
                    "fisAcik": "MALVERENDEN",
                    "toBarcode": "2704439285463",
                    "toJson": {
                      "to_Hks_Adi": "DAĞITIM MERKEZİ"
                    },
                    "movementJson": {
                      "isleme_Tarihi": "21/12/2022 02:19:30"
                    }
                  }
                ]
              }
            }
                """
    

    Notice how I removed, from your source, "data": since what you pasted is obviously not a JSON document. I guess, therefore, that this is where the problem occurs – something to do with the top or bottom of the JSON document or you need a container object around the JSON for Data

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search