-2

How To Parse Response of Multiple Types?

Key is like (suppose student_list is a key of list types when student_list is empty then it makes as a string like student_list=""), How to manage this types of response using Retrofit? I am using MVVM Model with retrofit.

My Response:

when I get Data into the List

{
    "status": 200,
    "data": [
        {
            "prod_month_total": 2989.61,
            "product": "GAS"
        },
        {
            "prod_month_total": 39566.22,
            "product": "OIL"
        },
        {
            "prod_month_total": 83912.55,
            "product": "OTHER"
        }
    ]
}

when List is Empty Then Response:

{"status":404,"data":"No result found"}

I am getting this Error:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 23 path $.data

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Please add some code samples to make us understand better. – Atish Agrawal Feb 20 '20 at 11:22
  • thanks for @AtishAgarwal, This is my response which is getting from server when I get Data into the List { "status": 200, "data": [ { "prod_month_total": 2989.61, "product": "GAS" }, { "prod_month_total": 39566.22, "product": "OIL" }, { "prod_month_total": 83912.55, "product": "OTHER" } ] }, and when List is Empty Then Response : {"status":404,"data":"No result found"} , and I created model class according to my response. – Praveen Thakur Feb 20 '20 at 11:33
  • 1
    Does this answer your question? [How to Handle Two Different Response in Retrofit](https://stackoverflow.com/questions/51238140/how-to-handle-two-different-response-in-retrofit) – jeprubio Feb 20 '20 at 11:40

1 Answers1

0

first create the right model calss use this site http://www.jsonschema2pojo.org

than use

`if( reponce.isSucessful){

if(responce.status==200){
   //code here 
  }
else{
 // find the error
}
 }else
 {
//
 }`
Amit pandey
  • 1,149
  • 1
  • 4
  • 15