I'm new with the retrofit library and I use version 2.4.0 I have an application where I want to show the following Json information in a ListView. Investigating I found that I expect a matrix but I receive a Json object but I have not been able to solve it for more than I have tried, does anyone have any ideas?
[
{
"idalumno":"1",
"nombre":"nombre 1",
"direccion":"direccion 1"
},
{
"idalumno":"2",
"nombre":"nomnbre 2",
"direccion":"direccion 2"
},
{
"idalumno":"3",
"nombre":"nombre 3",
"direccion":"direccion 3"
},
]
When running the application I find the following error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ and I have the following code:
public class ApiClient {
public static final String BASE_URL = "http://192.198.1.70:8080/ws/";
public static Retrofit retrofit = null;
public static Retrofit getApiclient(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
I use the interface
public interface ApiInterface {
@GET("obtener_alumnos.php")
Call<List<Contact>> getContacts();
}
My object Contact
public class Contact {
@SerializedName("name")
private String nombre;
private String direccion;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getDireccion() {
return direccion;
}
public void setDireccion(String direccion) {
this.direccion = direccion;
}
}
The OnCreate of my application
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView = findViewById(R.id.listView);
apiInterface = ApiClient.getApiclient().create(ApiInterface.class);
retrofit2.Call<List<Contact>> call = apiInterface.getContacts();
call.enqueue(new Callback<List<Contact>>() {
@Override
public void onResponse(retrofit2.Call<List<Contact>> call, Response<List<Contact>> response) {
contactList= response.body();
adapter = new NewAdapter(getApplicationContext(),R.layout.list_item, contactList);
ListView.setAdapter(adapter);
}
@Override
public void onFailure(retrofit2.Call<List<Contact>> call, Throwable t) {
Log.d("ERROR", t.getMessage());
//Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}