json 在 android 中使用改造时出现解析错误

json parsing error using retrofit in android

您好,我在解析过程中遇到问题 json。

我遇到了这个错误

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

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

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

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

这是我的 class 代码,它是 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
        }
    }
}

这是我使用 retrofit

调用 api 的代码

MAgencyRepo class

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

改装服务Class

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

我发现我放在代码中的 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 连接日志。