Home > Software design >  JsonSyntaxException: java.lang.NumberFormatException: empty String
JsonSyntaxException: java.lang.NumberFormatException: empty String

Time:02-02

I am trying to load a list of categories into a recycler view . I have created the POJO from Json body using POJO genertor. But when i try to get the rersponse it give me the following error : com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String. I can't seem to find why the error is coming , and before marking it as duplicate please see the attached code :

Json File

{
"status": true,
"code": 200,
"data": {
    "categories": [
        {
            "id": 2,
            "app_order": 2,
            "catId": 5,
            "catName": "Washing Machine",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/418441626002127.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 4,
            "app_order": 3,
            "catId": 24,
            "catName": "SANITIZE CLEANING",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/486301626002234.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 5,
            "app_order": 4,
            "catId": 8,
            "catName": "RO & Water Purifier",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/350671626002581.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 1,
            "app_order": 6,
            "catId": 1,
            "catName": "Air Conditioner",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/968621635528373.png",
            "headerFileType": "image",
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 7,
            "app_order": 6,
            "catId": 12,
            "catName": "Kitchen Clean",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/949491626003508.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 8,
            "app_order": 7,
            "catId": 18,
            "catName": "Plumbing",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/199891626003693.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 9,
            "app_order": 8,
            "catId": 19,
            "catName": "Chimney",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/158251626003741.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 10,
            "app_order": 9,
            "catId": 17,
            "catName": "Carpenter",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/900611626003785.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        },
        {
            "id": 11,
            "app_order": 10,
            "catId": "",
            "catName": "Cleaning",
            "type": 2,
            "navigateToPage": "Service",
            "imageIcon": "https://cumuluson.com/uploads/category/578281626003939.png",
            "headerImage": "https://cumuluson.com/uploads/category/",
            "colorBackground": "white"
        },
        {
            "id": 12,
            "app_order": 11,
            "catId": "",
            "catName": "Appliance",
            "type": 1,
            "navigateToPage": "Service",
            "imageIcon": "https://cumuluson.com/uploads/category/376691626004097.png",
            "headerImage": "https://cumuluson.com/uploads/category/",
            "colorBackground": "white"
        },
        {
            "id": 6,
            "app_order": 12,
            "catId": 4,
            "catName": "Refrigerator",
            "type": "",
            "navigateToPage": "ServiceFeature",
            "imageIcon": "https://cumuluson.com/uploads/category/730821626002736.png",
            "headerFileType": null,
            "headerImage": "",
            "colorBackground": "white"
        }
    ]
},
"error": null
}

Response Class

public class HomeCatResponse {
private int code;
private HomeCatData data;
private Object  error;
private boolean status;

public int getCode(){
    return code;
}

public HomeCatData getData(){
    return data;
}

public Object getError(){
    return error;
}

public boolean isStatus(){
    return status;
}

@Override
public String toString(){
    return
        "HomeCatResponse{"  
        "code = '"   code   '\''  
        ",homeCatData = '"   data   '\''  
        ",error = '"   error   '\''  
        ",status = '"   status   '\''  
        "}";
    }
   }

Data Class

public class HomeCatData {
private List<Category> categories;

public List<Category> getCategories(){
    return categories;
}

@Override
public String toString(){
    return 
        "HomeCatData{"  
        "categories = '"   categories   '\''   
        "}";
    }
}

Category Class

public class Category {
private int catId;
private String colorBackground;
private String headerFileType;
private String headerImage;
private String catName;
private String imageIcon;
private String navigateToPage;
private int appOrder;
private int id;
private String type;

public int getCatId(){
    return catId;
}

public String getColorBackground(){
    return colorBackground;
}

public String getHeaderFileType(){
    return headerFileType;
}

public String getHeaderImage(){
    return headerImage;
}

public String getCatName(){
    return catName;
}

public String getImageIcon(){
    return imageIcon;
}

public String getNavigateToPage(){
    return navigateToPage;
}

public int getAppOrder(){
    return appOrder;
}

public int getId(){
    return id;
}

public String getType(){
    return type;
}

@Override
public String toString(){
    return 
        "Category{"  
        "catId = '"   catId   '\''   
        ",colorBackground = '"   colorBackground   '\''   
        ",headerFileType = '"   headerFileType   '\''   
        ",headerImage = '"   headerImage   '\''   
        ",catName = '"   catName   '\''   
        ",imageIcon = '"   imageIcon   '\''   
        ",navigateToPage = '"   navigateToPage   '\''   
        ",app_order = '"   appOrder   '\''   
        ",id = '"   id   '\''   
        ",type = '"   type   '\''   
        "}";
    }
  }

The request using retrofit

private void initRetrofit() {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Config.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();
    api = retrofit.create(UrbanLiveApi.class);
}

private void getCategories() {
    Call<HomeCatResponse> call = api.getHomeCategories();
    call.enqueue(new Callback<HomeCatResponse>() {
        @Override
        public void onResponse(Call<HomeCatResponse> call, Response<HomeCatResponse> response) {
            if (!response.isSuccessful()) {
                showToast("Error "   response.errorBody()   response.toString());
                Log.d(TAG, response.errorBody()   response.message()   response.toString());
                return;
            }
            HomeCatResponse categories = response.body();
            showToast(categories.getData().getCategories().get(0).getCatName());
            showCategories(categories.getData().getCategories());
        }

        @Override
        public void onFailure(Call<HomeCatResponse> call, Throwable t) {
            Log.d(TAG, t.getMessage());
            Log.d(TAG, t.toString());
            showToast(t.getMessage());
        }
    });
}

Any help would be much appreciated .

CodePudding user response:

There are multiple entries where catId is an empty string, e.g.:

{
        "id": 11,
        "app_order": 10,
        "catId": "", <-- should be a number
        "catName": "Cleaning",
        "type": 2,
        "navigateToPage": "Service",
        "imageIcon": "https://cumuluson.com/uploads/category/578281626003939.png",
        "headerImage": "https://cumuluson.com/uploads/category/",
        "colorBackground": "white"
    },
    {
        "id": 12,
        "app_order": 11,
        "catId": "", <-- should be a number
        "catName": "Appliance",
        "type": 1,
        "navigateToPage": "Service",
        "imageIcon": "https://cumuluson.com/uploads/category/376691626004097.png",
        "headerImage": "https://cumuluson.com/uploads/category/",
        "colorBackground": "white"
    }

Your parser expects a number for that field, which means it can't deal with an empty string. Make sure that catId is always a number in your json.

CodePudding user response:

Check whether any property is null for example catId should be string if response is empty string

private String catId;
  •  Tags:  
  • Related