I'm trying to consume data from an API using retrofit, however whenever I try to connect it returns that error. I'm using a class that is an Array of these objects (SisalfaCatalog) and throwing the return inside it, but it does not work. Here is the code:
The service interface:
public interface SisalfaService {
public static final String BASE_URL = "https://app.sisalfa.dcx.ufpb.br/v1/api/";
@GET("contexts")
Call<SisalfaCatalog> listPalavras();
}
The class with the Array of objects:
public class SisalfaCatalog {
public ArrayList<Palavra> palavras;
}
API Content:
[
{
"id": 1,
"name": "Sala",
"image": "https://app.sisalfa.dcx.ufpb.br/v1/static/images/dababcd96ab5f7854f820ff926c9acaa.jpg",
"sound": "",
"video": "",
"createdAt": "2018-06-22T19:31:18.745Z",
"updatedAt": "2018-06-22T19:31:18.745Z",
"user": {
"id": 1,
"username": "neto",
"email": "[email protected]",
"firstName": "José",
"lastName": "Antonio da Silva Neto",
"photo": null
}
},
And the main with the service call:
retrofit = new Retrofit.Builder()
.baseUrl(SisalfaService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
SisalfaService service = retrofit.create(SisalfaService.class);
Call<SisalfaCatalog> call = service.listPalavras();
call.enqueue(new Callback<SisalfaCatalog>() {
@Override
public void onResponse(Call<SisalfaCatalog> call, Response<SisalfaCatalog> response) {
if(!response.isSuccessful()) {
Toast.makeText(MainActivity.this, "Errr, a conexão não deu certo", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Bem, deu certo aparentemente", Toast.LENGTH_LONG).show();
SisalfaCatalog catalog = response.body();
for(Palavra p : catalog.palavras) {
Log.i("Nome: ", p.getName());
Log.i("Id: ", ""+p.getId());
Log.i("Url: ", p.getImage());
}
}
}
@Override
public void onFailure(Call<SisalfaCatalog> call, Throwable t) {
Toast.makeText(MainActivity.this, "Errr, não deu certo. "+t.getMessage(), Toast.LENGTH_LONG).show();
Log.i("Erro: ", t.getMessage());
}
});