2

Hello I am trying to use retrofit But I am getting this error:-

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

Here is my MainActivity

public class MainActivity extends AppCompatActivity {

@Bind(R.id.activity_main_tv_display)
TextView textData;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
}
@OnClick(R.id.activity_main_btn_show)
void press() {
    RemoteApi.Factory.getInstance().getModel().enqueue(new Callback<Model>() {
        @Override
        public void onResponse(Call<Model> call, Response<Model> response) {
            textData.setText(response.toString());
            Log.e("--success--", String.valueOf(response));
        }
        @Override
        public void onFailure(Call<Model> call, Throwable t) {
            Log.e("--fail--", t.getMessage());
        }
    });
  }
}

This is my model

public class Model {

@SerializedName("Title")
@Expose
private String Title;
@SerializedName("Message")
@Expose
private String Message;
@SerializedName("id")
@Expose
private int id;
// getters and setters declare
}

Here is my interface

public interface RemoteApi {

String BASE_URL = "xyz/";
@GET("api/Cards")
Call<Model> getModel();
class Factory {
    public static RemoteApi remoteApi;
    public static RemoteApi getInstance() {
            Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(BASE_URL)
                    .build();
            remoteApi = retrofit.create(RemoteApi.class);
            return remoteApi;
        }
    }
}

And My API looks like this

[{
  "Title": "xyz",
  "Message": "hello",
  "id": 1
}, {
  "Title": "abc",
  "Message": "hello",
  "id": 2
}] 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
shivam
  • 445
  • 1
  • 8
  • 22
  • 1
    Your API is returning a JSON Array. Retrofit is expecting `Callback`. Try `Callback>` – OneCricketeer Mar 22 '16 at 18:43
  • can you explain more @cricket_007 please – shivam Mar 22 '16 at 18:47
  • Data within `{}` is a JSON object. And data within `[]` is a JSON array. Look at the first character of your API. It is a `[`. Thus, your error is reporting that it "`was BEGIN_ARRAY`", and not "`BEGIN_OBJECT`". – OneCricketeer Mar 22 '16 at 18:49
  • But when I am editing my interface from Call getModel() to Callback> getModel(). It is creating error in my MainActivity ie cannot resolve method enqueue @cricket_007 – shivam Mar 22 '16 at 18:51
  • That's because you have to change `Model` to `List` in all places you have `Callback` and `Call`. – OneCricketeer Mar 22 '16 at 18:57

1 Answers1

0

You want to get a List of objects from your API (by looking at the first character), but your error states it expected an Object (also, by looking at the first character). You are using Call<Model> in your interface, which states you only want one Model object returned, not a list of them.

Try setting your interface like so

public interface RemoteApi {

    String BASE_URL = "xyz/";
    @GET("api/Cards")
    Call<List<Model>> getModel();

And the other code like this

RemoteApi.Factory.getInstance().getModel().enqueue(new Callback<List<Model>>() {
        @Override
        public void onResponse(Call<List<Model>> call, Response<List<Model>> response) {
            String responseString = String.valueOf(response);
            textData.setText(responseString);
            Log.e("--success--", responseString);
        }
        @Override
        public void onFailure(Call<List<Model>> call, Throwable t) {
            Log.e("--fail--", t.getMessage());
        }
    });
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • But it is display retrofit2.Response@41ce5630 not the result @cricket_007 – shivam Mar 22 '16 at 19:10
  • Yes, that's because you are doing `String.valueOf(response)`. Try `response.body()` instead of `response` – OneCricketeer Mar 22 '16 at 19:15
  • Log.e is displaying [examples.sewoyebah.com.retrofit.model.Model@41cfd158, examples.sewoyebah.com.retrofit.model.Model@41c5ee70] – shivam Mar 22 '16 at 19:22
  • Yup, that is a list of your model objects. Please refer to [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – OneCricketeer Mar 22 '16 at 19:24