简体   繁体   中英

java.lang.IllegalStateException in retrofit

I am using retrofit 2.0 Gson data not getting

When i check Postman i am getting response below

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

Error:

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

APIClient Client

public static Retrofit getClient() {

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

    }

This is Pojo class

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

Activity class

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

I found some solution as google suggestion but i donot know how to achieve.

You problem is java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

So you should check you response of your code .

Your JSON response is JSONObject ,so you should use Call<Appuser_Pojoclass> call

Change

Call<List<Appuser_Pojoclass>> call

to

Call<Appuser_Pojoclass> call

The error is in the Call declaration,change

     Call<List<Appuser_Pojoclass>>

with

      Call<Appuser_Pojoclass>

Your retrofit call response type is <List<Appuser_Pojoclass>> but your API response is not a list of Appuser_Pojoclass , it is simply a single object of type Appuser_Pojoclass .

Change type from <List<Appuser_Pojoclass>> to Appuser_Pojoclass in your retrofit call

You probably defined your get_AppUsers method as something that returns Call<List<Appuser_Pojoclass>> instead of just Call<Appuser_Pojoclass> so it ends up looking for a JSON array, but then your response only contains a single JSON object which causes the error. Change get_AppUsers to return Call<Appuser_Pojoclass> and then change your callback accordingly and that should fix the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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