[英]JsonSyntaxException: java.lang.NumberFormatException: empty String
I am trying to load a list of categories into a recycler view.我正在尝试将类别列表加载到回收站视图中。 I have created the POJO from Json body using POJO genertor.
我使用 POJO 生成器从 Json 主体创建了 POJO。 But when i try to get the rersponse it give me the following error: com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String.
但是当我尝试得到回复时,它给了我以下错误:com.google.gson.JsonSyntaxException: java.lang. I can't seem to find why the error is coming, and before marking it as duplicate please see the attached code:
我似乎无法找到错误出现的原因,在将其标记为重复之前,请查看随附的代码:
Json File
Json 文件
{
"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
响应 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
数据 Class
public class HomeCatData {
private List<Category> categories;
public List<Category> getCategories(){
return categories;
}
@Override
public String toString(){
return
"HomeCatData{" +
"categories = '" + categories + '\'' +
"}";
}
}
Category Class
类别 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
使用 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.任何帮助将非常感激。
Check whether any property is null for example catId should be string if response is empty string检查是否有任何属性是 null 例如如果响应为空字符串,则 catId 应为字符串
private String catId;
There are multiple entries where catId
is an empty string, eg:有多个条目,其中
catId
为空字符串,例如:
{
"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.确保
catId
在您的 json 中始终是一个数字。
I got the same error recently and i got it solved when i changed the variable type from int to string.Hope it will help you too.我最近遇到了同样的错误,当我将变量类型从 int 更改为 string 时,我得到了解决。希望它也能帮助你。
Change private int catId;更改私有 int catId; to: private String catId;
至:私有字符串 catId;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.