Digvijay
Digvijay

Reputation: 3281

JSON data is not fetching using Retrofit

I am using retrofit networking library for fetching data from server.I am using MVVM architecture model to create an app.When app starts it is showing some exception and not fetching data.

java.lang.IllegalStateException:Expected BEGIN_OBJECT but was BEGIN_ARRAY at Line 1 Column 2 path $.

I am fail to understand what it is trying to say.Below is my code.

RetrofitClient.java

public class RetrofitClient {

private static Retrofit retrofit = null;
private static final String URL = "https://example.com/";

public static Retrofit getInstance(){

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                              .connectTimeout(20, TimeUnit.SECONDS)
                              .readTimeout(20,TimeUnit.SECONDS)
                              .writeTimeout(20,TimeUnit.SECONDS)
                              .build();

    if(retrofit == null){

        retrofit = new Retrofit.Builder()
                   .baseUrl(URL)
                   .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
                   .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                   .client(okHttpClient)
                   .build();
    }

    return retrofit;
  }

private RetrofitClient(){

  }
}

ApiService.class

public interface ApiService {

    @GET("getUsers")
    Call<User> getUser();
}

User.java

public class User {

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

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

public User(String name, String age) {
    this.name = name;
    this.age = age;
 }

public String getName() {
    return name;
 }

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

public String getAge() {
    return age;
 }

public void setAge(String age) {
    this.age = age;
 }
}

UserRepository.java

public class UserRepository {

Context context;

public UserRepository(Context context) {
    this.context = context;
}

public void getUserList(){

          Retrofit retrofit = RetrofitClient.getInstance();
          ApiService apiService = retrofit.create(ApiService.class);

          Call<User> userList = apiService.getUser();

          userList.enqueue(new Callback<User>() {
              @Override
              public void onResponse(Call<User> call, Response<User> response) {

             Log.d("Response", String.valueOf(response.body()));
       }

              @Override
              public void onFailure(Call<User> call, Throwable t) {

                  Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
              }
          });
  }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

UserRepository userRepository;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(MainActivity.this,AddUser.class);
            startActivity(i);
        }
    });

    userRepository = new UserRepository(this);

    userRepository.getUserList();
}

API Response

[
  {
    "name": "Link",
    "age": "5"
  },
  {
    "name": "Rhea",
    "age": "24"
  },
  {
    "name": "Don",
    "age": "10"
  },
  {
    "name": "oman",
    "age": "30"
  },
  {
    "name": "Ron",
    "age": "18"
  },
  {
    "name": "roman",
    "age": "25"
  }
]

Someone please let me know why above exception is showing am I doing something wrong. Any help would be appreciated.

THANKS

Upvotes: 0

Views: 60

Answers (3)

Martin Zeitler
Martin Zeitler

Reputation: 76807

Isn't expected BEGIN_OBJECT but was BEGIN_ARRAY explict?

It is because that array has no name and it starts with [ instead of { ...

valid JSON (as expected by GSON) would look alike this:

{
    "users": [
        {"name": "Link","age": "5"},
        {"name": "Rhea","age": "24"},
        {"name": "Don","age": "10"},
        {"name": "oman", "age": "30"},
        {"name": "Ron", "age": "18"},
        {"name": "roman", "age": "25"}
    ]
}

If you cannot change the API, then intercept the call and fix the input JSON, before mapping it.

The return type is also wrongful, because that should be ArrayList<User>.

PS: that top-level array users would need it's own data model.

Upvotes: 0

Mojtaba Haddadi
Mojtaba Haddadi

Reputation: 1376

in your ApiService Interface you wrote this

@GET("getUsers")
Call<User> getUser();

but based on your json responce really you need a list of Users , not a single user

@GET("getUsers")
Call<List<User>> getUser();

also in UserRepository class you should have something like this.

public class UserRepository {

Context context;

public UserRepository(Context context) {
    this.context = context;
}

public void getUserList(){

      Retrofit retrofit = RetrofitClient.getInstance();
      ApiService apiService = retrofit.create(ApiService.class);

      Call<List<User>> userList = apiService.getUser();

      userList.enqueue(new Callback<List<User>>() {
          @Override
          public void onResponse(Call<List<User>> call, Response<List<User>> response) {

         Log.d("Response", String.valueOf(response.body()));
   }

          @Override
          public void onFailure(Call<List<User>> call, Throwable t) {

              Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
          }
      });
 }
}

Upvotes: 1

Parag Agrawal
Parag Agrawal

Reputation: 97

Your JSON data is an array of user objects, but you are trying to parse a single User Object. Use List to indicate that you want to parse an array of User objects, something like below in your UserRepository class:

public void getUserList() {

          Retrofit retrofit = RetrofitClient.getInstance();
          ApiService apiService = retrofit.create(ApiService.class);

          Call<List<User>> userList = apiService.getUser();

          userList.enqueue(new Callback<List<User>>() {
              @Override
              public void onResponse(Call<List<User>> call, Response<List<User>> response) {

             Log.d("Response", String.valueOf(response.body()));
       }

              @Override
              public void onFailure(Call<List<User>> call, Throwable t) {

                  Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
              }
          });
  }

Also your ApiService interface should reflect that.

public interface ApiService {

@GET("getUsers")
Call<List<User>> getUser();

}

Upvotes: 1

Related Questions