在android中使用改造的json解析错误

Posted

技术标签:

【中文标题】在android中使用改造的json解析错误【英文标题】:json parsing error using retrofit in android 【发布时间】:2021-11-02 10:13:39 【问题描述】:

您好,我在解析 json 时遇到问题。

我收到了这个错误

D/MAgencyRepo: onFailure : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

谷歌搜索此错误的结果表明我从 api 获得的响应与我所做的响应模型类不同。

所以我试图找到这两者之间的区别,但我找不到任何区别。

为什么会出现此错误? 我该如何解决??

这是我的 DTO 类代码

public class ShortWeather implements Serializable 

    @Expose
    @SerializedName("response")
    public Response response;

    public static class Response implements Serializable
        @Expose
        @SerializedName("body")
        public Body body;
        @Expose
        @SerializedName("header")
        public Header header;
    

    public static class Body implements Serializable
        @Expose
        @SerializedName("totalCount")
        public int totalcount;
        @Expose
        @SerializedName("numOfRows")
        public int numofrows;
        @Expose
        @SerializedName("pageNo")
        public int pageno;
        @Expose
        @SerializedName("items")
        public Items items;
        @Expose
        @SerializedName("dataType")
        public String datatype;
    

    public static class Items implements Serializable
        @Expose
        @SerializedName("item")
        public List<Item> item;
    

    public static class Item implements Serializable
        @Expose
        @SerializedName("ny")
        public int ny;
        @Expose
        @SerializedName("nx")
        public int nx;
        @Expose
        @SerializedName("fcstValue")
        public String fcstvalue;
        @Expose
        @SerializedName("fcstTime")
        public String fcsttime;
        @Expose
        @SerializedName("fcstDate")
        public String fcstdate;
        @Expose
        @SerializedName("category")
        public String category;
        @Expose
        @SerializedName("baseTime")
        public String basetime;
        @Expose
        @SerializedName("baseDate")
        public String basedate;
    

    public static class Header implements Serializable
        @Expose
        @SerializedName("resultMsg")
        public String resultmsg;
        @Expose
        @SerializedName("resultCode")
        public String resultcode;
    

    @Override
    public String toString() 
        return "ShortWeather" +
                "response=" + response +
                '';
    

这是我通过邮递员调用的 json 响应


    "response": 
        "header": 
            "resultCode": "00",
            "resultMsg": "NORMAL_SERVICE"
        ,
        "body": 
            "dataType": "JSON",
            "items": 
                "item": [
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "TMP",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "26",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "UUU",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "-1.8",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VVV",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "-0.4",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VEC",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "78",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "WSD",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "1.9",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SKY",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "1",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PTY",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "POP",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PCP",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "강수없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "REH",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "55",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SNO",
                        "fcstDate": "20210904",
                        "fcstTime": "1200",
                        "fcstValue": "적설없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "TMP",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "27",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "UUU",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "-1.2",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VVV",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "0.3",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VEC",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "103",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "WSD",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "1.3",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SKY",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "1",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PTY",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "POP",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PCP",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "강수없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "REH",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "50",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SNO",
                        "fcstDate": "20210904",
                        "fcstTime": "1300",
                        "fcstValue": "적설없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "TMP",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "27",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "UUU",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "-0.2",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VVV",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "0.6",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VEC",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "153",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "WSD",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "0.7",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SKY",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "3",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PTY",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "POP",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "20",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PCP",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "강수없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "REH",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "50",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SNO",
                        "fcstDate": "20210904",
                        "fcstTime": "1400",
                        "fcstValue": "적설없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "TMP",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "28",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "UUU",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "0.7",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VVV",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "0.6",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VEC",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "229",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "WSD",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "0.9",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SKY",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "3",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PTY",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "POP",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "20",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "PCP",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "강수없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "REH",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "50",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "SNO",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "적설없음",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "TMX",
                        "fcstDate": "20210904",
                        "fcstTime": "1500",
                        "fcstValue": "28.0",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "TMP",
                        "fcstDate": "20210904",
                        "fcstTime": "1600",
                        "fcstValue": "27",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "UUU",
                        "fcstDate": "20210904",
                        "fcstTime": "1600",
                        "fcstValue": "1.5",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VVV",
                        "fcstDate": "20210904",
                        "fcstTime": "1600",
                        "fcstValue": "0.1",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "VEC",
                        "fcstDate": "20210904",
                        "fcstTime": "1600",
                        "fcstValue": "266",
                        "nx": 55,
                        "ny": 127
                    ,
                    
                        "baseDate": "20210904",
                        "baseTime": "1100",
                        "category": "WSD",
                        "fcstDate": "20210904",
                        "fcstTime": "1600",
                        "fcstValue": "1.5",
                        "nx": 55,
                        "ny": 127
                    
                ]
            ,
            "pageNo": 1,
            "numOfRows": 50,
            "totalCount": 676
        
    

这是我使用改造调用 api 的代码

MAgencyRepo 类

public class MAgencyRepo 

    private final String TAG = "MAgencyRepo";
    private final static String BASE_URL = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/"; 
    private static MAgencyRepo instance;
    private Retrofit retrofit;
    private MeteorologicalAgencyAPI MaAPI;
    private ShortWeather sw;

    public static MAgencyRepo getInStance() 
        if(instance == null)
            instance = new MAgencyRepo();
        
        return instance;
    

    public MutableLiveData<ShortWeather> getWeather() 

        retrofit = new RetrofitService().getRetroInstance(BASE_URL);

        MaAPI = retrofit.create(MeteorologicalAgencyAPI.class);

        sw = new ShortWeather();
        MutableLiveData<ShortWeather> data = new MutableLiveData<ShortWeather>();
        callWeatherAPI(data);
        return data;
    

    private void callWeatherAPI(MutableLiveData<ShortWeather> data) 

        Call<ShortWeather> call = MaAPI.getShortWeather("this is my api key",
                50,1,"JSON","20210904","0500",55,127);

        call.enqueue(new Callback<ShortWeather>() 
            @Override
            public void onResponse(Call<ShortWeather> call, Response<ShortWeather> response) 
                if(response.isSuccessful()) 
                    data.postValue(response.body());
                    Log.i(TAG,"API CONNECT SUCCESS");
                    Log.i(TAG,response.body().toString());
                else 
                    Log.i(TAG,"API CONNECT SUCCESS BUT WRONG PARAMETER");
                    Log.i(TAG,response.body().toString());
                
            

            @Override
            public void onFailure(Call<ShortWeather> call, Throwable t) 
                Log.d(TAG,"onFailure : "+t.getMessage());
            
        );
    

改造服务类

public class RetrofitService 
    public Retrofit getRetroInstance(String baseUrl)

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    

【问题讨论】:

【参考方案1】:

我发现我放在代码上的 api 密钥已被编码。 但是当我尝试发送一个 api 密钥时,密钥又被编码了。 所以我一直收到一个错误提示

服务密钥未注册

我不知道如何获取 http 连接日志。 所以我不知道发生了什么 但我找到了一种使用 okhttp 从 http 连接获取日志的方法

这是我的代码

public class RetrofitService 
    public Retrofit getRetroInstance(String baseUrl)

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    

下次我应该使用 okhttp 来查看 http 连接日志。

【讨论】:

以上是关于在android中使用改造的json解析错误的主要内容,如果未能解决你的问题,请参考以下文章

改造解析JSON响应空值Android

使用改造将 json 解析为 POJO

如何通过改造解析 JSON 响应

使用改造的 JSON 解析

如何使用改造和 gson 异步解析 json

ArrayList<String> 使用改造 android 解析成 recyclerview