I made the server part on java, which by reference gives me JSON - http://139.59.164.239:8080/remindme.server2/reminders
I need to parse this JSON into an array of java objects and display all this in a listview in Android. I use Retrofit2 , and when I call the enqueue method enqueue I get to the onFailure method with the exception: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
My code is below:
1. MainActivity.java:
public class MainActivity extends AppCompatActivity { public static final String TAG = "TAG"; private ListView listView; private View parentView; private ArrayList<Contact> contactList; private ContactAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contactList = new ArrayList<>(); parentView = findViewById(R.id.parentLayout); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); assert fab != null; fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(@NonNull final View view) { /** * Checking Internet Connection */ if (InternetConnection.checkConnection(getApplicationContext())) { //Creating an object of our api interface ApiService api = RetroClient.getApiService(); Call<ContactList> call = api.getMyJSON(); Log.d(TAG, "" + call.isExecuted()); Log.d(TAG, "Я в после Call<ContactList> call = api.getMyJSON() "); call.enqueue(new Callback<ContactList>() { @Override public void onResponse(Call<ContactList> call, Response<ContactList> response) { Log.d(TAG, "Я в onResponse "); dialog.dismiss(); if (response.isSuccessful()) { contactList = response.body().getContacts(); adapter = new ContactAdapter(MainActivity.this, contactList); listView.setAdapter(adapter); } else { Log.d(TAG, "Я в else "); Snackbar.make(parentView, R.string.string_some_thing_wrong, Snackbar.LENGTH_LONG).show(); } } @Override public void onFailure(Call<ContactList> call, Throwable t) { dialog.dismiss(); Log.d(TAG, "Я в onFailure" + t.getMessage().toString()); } }); } else { Snackbar.make(parentView, R.string.string_internet_connection_not_available, Snackbar.LENGTH_LONG).show(); } } }); } } 2. Contact.java
public class Contact { @SerializedName("id") @Expose private String id; @SerializedName("title") @Expose private String title; @SerializedName("remindDate") @Expose private String remindDate; /** * @return The id */ public String getId() { return id; } /** * @return The name */ public String getTitle() { return title; } /** * @return The email */ public String getRemindDate() { return remindDate; } } 3. ContactList.java
public class ContactList { //@SerializedName("remind") @Expose private ArrayList<Contact> contacts = new ArrayList<>(); /** * @return The contacts */ public ArrayList<Contact> getContacts() { return contacts; } /** * @param contacts The contacts */ public void setContacts(ArrayList<Contact> contacts) { this.contacts = contacts; } } 4. RetroClient.java
public class RetroClient { private static final String ROOT_URL = "http://139.59.164.239:8080"; private static Retrofit getRetrofitInstance() { return new Retrofit.Builder() .baseUrl(ROOT_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } public static ApiService getApiService() { return getRetrofitInstance().create(ApiService.class); } } 5. ApiService.java
public interface ApiService { @GET("/remindme.server2/reminders") Call<ContactList> getMyJSON(); } As a result, there is an error in the OnFailure method: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ I am new to this topic, please tell me what the problem is and how to solve it. Thank!