改造中的java.lang.IllegalStateException

时间:2017-10-24 10:00:33

标签: java android retrofit

我正在使用改装2.0 Gson数据未获取

当我查看Postman时,我收到的回复

{
    "name": "Yashodhan Communication",
    "zone": "9-Belgaum",
    "tsm_name": "Tarun Patil",
    "asm_name": "Shivakumar Patil"
}

错误:

  

java.lang.IllegalStateException:预期BEGIN_ARRAY但是   BEGIN_OBJECT在第1行第2列路径$

APIClient客户

public static Retrofit getClient() {

        retrofit = new Retrofit.Builder()
                .baseUrl("http://vehiclerescue.in/ideadarpan_beta/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;

    }

这是 Pojo类

public class Appuser_Pojoclass {


    @SerializedName("name")
    @Expose
    private  String name;

    @SerializedName("zone")
    @Expose
    private  String zone;

    @SerializedName("tsm_name")
    @Expose
    private  String tsm_name;

    @SerializedName("asm_name")
    @Expose
    private  String  asm_name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
    }

    public String getTsm_name() {
        return tsm_name;
    }

    public void setTsm_name(String tsm_name) {
        this.tsm_name = tsm_name;
    }

    public String getAsm_name() {
        return asm_name;
    }

    public void setAsm_name(String asm_name) {
        this.asm_name = asm_name;
    }
}

活动类

 private void fetchAllAppUserdata()
    {

        progressBar.setVisibility(View.VISIBLE);

        Log.d("getauthkeydisplay","**** "+Idea_Urban.getInstance().get_Authkeyvalues());

        ideaInterface.get_AppUsers(Idea_Urban.getInstance().get_Authkeyvalues()).enqueue(new Callback<List<Appuser_Pojoclass>>() {
            @Override
            public void onResponse(Call<List<Appuser_Pojoclass>> call, Response<List<Appuser_Pojoclass>> response) {

                if(app_users != null)
                {
                    progressBar.setVisibility(View.INVISIBLE);
                    app_users.clear();
                }
                if(response.body()!=null) {

                    app_users.addAll(response.body());

                    Log.d("appuserresponce","*****   "+response.body());
                //    app_userDetailsAdapter.notifyDataSetChanged();
                }else
                {
                    progressBar.setVisibility(View.INVISIBLE);
                }


            }

            @Override
            public void onFailure(Call<List<Appuser_Pojoclass>> call, Throwable t) {
                Log.d("failure","**** ");

                Log.d("printmetthodsfailure","faiure"+call.toString()   + "\n "  + t.toString());

                progressBar.setVisibility(View.INVISIBLE);
            }
        });
    }

我找到了一些谷歌建议的解决方案,但我不知道如何实现。

4 个答案:

答案 0 :(得分:2)

您的问题是java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

所以你应该检查你对代码的回应。

您的JSON回复是JSONObject,因此您应该使用Call<Appuser_Pojoclass> call

更改

Call<List<Appuser_Pojoclass>> call

Call<Appuser_Pojoclass> call

答案 1 :(得分:2)

错误在Call声明中,更改

     Call<List<Appuser_Pojoclass>>

      Call<Appuser_Pojoclass>

答案 2 :(得分:1)

您的改装呼叫响应类型为<List<Appuser_Pojoclass>>,但您的API响应不是Appuser_Pojoclass的列表,它只是Appuser_Pojoclass类型的单个对象。

在改装电话中将类型从<List<Appuser_Pojoclass>>更改为Appuser_Pojoclass

答案 3 :(得分:1)

您可能将get_AppUsers方法定义为返回Call<List<Appuser_Pojoclass>>而非Call<Appuser_Pojoclass>的方法,因此它最终会查找JSON数组,但您的响应只包含一个JSON对象这会导致错误。将get_AppUsers更改为返回Call<Appuser_Pojoclass>,然后相应地更改回调,这样可以解决问题。