翻新-> java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列$处为BEGIN_OBJECT [英] Retrofit --> java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

查看:109
本文介绍了翻新-> java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列$处为BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现的任何帖子都没有帮助我了解我应该做什么.坚持了很长一段时间,所以在这里问.我希望IN和OUT名称列表为"in"和"out"是json数组.该错误在响应的OnFailure中显示为异常. getUsers()在主要活动中.

None of the posts i found have helped me understand what im supposed to do. Stuck on this for quite a while so asking here. I want the List of IN and OUT names as "in" and "out" are json arrays. The error shows up as exception in OnFailure of response. getUsers() is in main activity.

private void getUsers() {


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    APIInterface api = retrofit.create(APIInterface.class);

    Call<List<In>> call1 = api.getInUsers();
    call1.enqueue(new Callback<List<In>>() {
        @Override
        public void onResponse(Call<List<In>> call1, Response<List<In>> response) {
            List<In> inUsers = response.body();
            Log.v("InUsers",String.valueOf(inUsers));
            data.add(new Vote(Vote.HEADER_TYPE,"IN"));
            mAdapter.notifyDataSetChanged();
            for(In vote : inUsers) {
                data.add(new Vote(Vote.ITEM_TYPE,String.valueOf(vote)));
            }


        }

        @Override
        public void onFailure(Call<List<In>> call1, Throwable t) {
            Toast.makeText(getActivity().getApplicationContext(), t.getMessage() + "IN LIST", Toast.LENGTH_LONG).show();
        }
    });
}

json是:

 {
 "message": "Hello World.",
 "planData": {
     "planId":"44444410",
     "votesData": {
         "votes": {
             "in":[ 
                 {
                     "firstName" : "Raj",
                     "lastName" : "Taj"
                 },
                 {
                     "firstName" : "Badger",
                     "lastName" : "Tagger"
                 },
                  {
                     "firstName" : "Candle",
                     "lastName" : "Bangle"
                 }
                 ] , 
                 "out" : [
                     {
                     "firstName" : "Bonnie",
                     "lastName" : "Clooney"
                 },
                 {
                     "firstName" : "Clyde",
                     "lastName" : "Hyde"
                 }
                 ]
         }
     }
 }
}

界面:

public interface APIInterface {
@GET("/")
Call<List<In>> getInUsers();

    @GET("/")
    Call<List<Out>> getOutUsers();
}

我从 http://www.jsonschema2pojo.org/

在示例类中,只有message和planId.我最初在接口中调用示例类.但认为错误可能是因为该错误,因此将其更改为List,但什么也没有.

in example class there was only message and planId. I was calling the example class in interface initially. but thought the error might be because of that so changed it to List but nothing.

该站点的POJO代码:

POJO code from the site:

>  
> -----------------------------------com.example.Example.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class Example {
> 
> @SerializedName("message") @Expose private String message;
> @SerializedName("planData") @Expose private PlanData planData;
> 
> public String getMessage() { return message; }
> 
> public void setMessage(String message) { this.message = message; }
> 
> public PlanData getPlanData() { return planData; }
> 
> public void setPlanData(PlanData planData) { this.planData = planData;
> }
> 
> }
> -----------------------------------com.example.In.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class In {
> 
> @SerializedName("firstName") @Expose private String firstName;
> @SerializedName("lastName") @Expose private String lastName;
> 
> public String getFirstName() { return firstName; }
> 
> public void setFirstName(String firstName) { this.firstName =
> firstName; }
> 
> public String getLastName() { return lastName; }
> 
> public void setLastName(String lastName) { this.lastName = lastName; }
> 
> }
> -----------------------------------com.example.Out.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class Out {
> 
> @SerializedName("firstName") @Expose private String firstName;
> @SerializedName("lastName") @Expose private String lastName;
> 
> public String getFirstName() { return firstName; }
> 
> public void setFirstName(String firstName) { this.firstName =
> firstName; }
> 
> public String getLastName() { return lastName; }
> 
> public void setLastName(String lastName) { this.lastName = lastName; }
> 
> }
> -----------------------------------com.example.PlanData.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class PlanData {
> 
> @SerializedName("planId") @Expose private String planId;
> @SerializedName("votesData") @Expose private VotesData votesData;
> 
> public String getPlanId() { return planId; }
> 
> public void setPlanId(String planId) { this.planId = planId; }
> 
> public VotesData getVotesData() { return votesData; }
> 
> public void setVotesData(VotesData votesData) { this.votesData =
> votesData; }
> 
> }
> -----------------------------------com.example.Votes.java-----------------------------------
> 
> package com.example;
> 
> import java.util.List; import com.google.gson.annotations.Expose;
> import com.google.gson.annotations.SerializedName;
> 
> public class Votes {
> 
> @SerializedName("in") @Expose private List<In> in = null;
> @SerializedName("out") @Expose private List<Out> out = null;
> 
> public List<In> getIn() { return in; }
> 
> public void setIn(List<In> in) { this.in = in; }
> 
> public List<Out> getOut() { return out; }
> 
> public void setOut(List<Out> out) { this.out = out; }
> 
> }
> -----------------------------------com.example.VotesData.java-----------------------------------
> 
> package com.example;
> 
> import com.google.gson.annotations.Expose; import
> com.google.gson.annotations.SerializedName;
> 
> public class VotesData {
> 
> @SerializedName("votes") @Expose private Votes votes;
> 
> public Votes getVotes() { return votes; }
> 
> public void setVotes(Votes votes) { this.votes = votes; }
> 
> }

推荐答案

您必须执行以下操作.

public interface APIInterface {
@GET("/")
Call<Example> getInUsers();

    @GET("/")
    Call<List<Out>> getOutUsers();
}

您的活动如下.

Call<Example> call1 = api.getInUsers();
    call1.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example> call1, Response<Example> response) {
            List<In> inUsers = response. getPlanData(). getVotesData().getVotes().getIn();
            Log.v("InUsers",String.valueOf(inUsers));
            data.add(new Vote(Vote.HEADER_TYPE,"IN"));
            mAdapter.notifyDataSetChanged();
            for(In vote : inUsers) {
                data.add(new Vote(Vote.ITEM_TYPE,String.valueOf(vote)));
            }


        }

        @Override
        public void onFailure(Call<Example> call1, Throwable t) {
            Toast.makeText(getActivity().getApplicationContext(), t.getMessage() + "IN LIST", Toast.LENGTH_LONG).show();
        }
    });

这篇关于翻新-> java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列$处为BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆