繁体   English   中英

JsonSyntaxException:java.lang.NumberFormatException:空字符串

[英]JsonSyntaxException: java.lang.NumberFormatException: empty String

我正在尝试将类别列表加载到回收站视图中。 我使用 POJO 生成器从 Json 主体创建了 POJO。 但是当我尝试得到回复时,它给了我以下错误:com.google.gson.JsonSyntaxException: java.lang. 我似乎无法找到错误出现的原因,在将其标记为重复之前,请查看随附的代码:

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
}

响应 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 + '\'' +
        "}";
    }
   }

数据 Class

public class HomeCatData {
private List<Category> categories;

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

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

类别 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 + '\'' + 
        "}";
    }
  }

使用 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());
        }
    });
}

任何帮助将非常感激。

检查是否有任何属性是 null 例如如果响应为空字符串,则 catId 应为字符串

private String catId;

有多个条目,其中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"
    }

您的解析器需要该字段的数字,这意味着它无法处理空字符串。 确保catId在您的 json 中始终是一个数字。

我最近遇到了同样的错误,当我将变量类型从 int 更改为 string 时,我得到了解决。希望它也能帮助你。

更改私有 int catId; 至:私有字符串 catId;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM