0
I’m trying to connect to the local network, using the RetroFit
. In the local network I have a Rest that returns a json, but when I run the event to make it happen, an error happens
I have an interface class for Retrofit
public interface iRetroFitSibcom {
@GET("/{categoria}")
Call<List<Categoria>> getCategorias(@Path("categoria") String categoria);
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:8080/sibcomslim")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
And a button where the event takes place
@Override
public void onClick(View v) {
iRetroFitSibcom githubUser = iRetroFitSibcom.retrofit.create(iRetroFitSibcom.class);
final Call <List<Categoria>> call = githubUser.getCategorias("categorias");
call.enqueue(new Callback<List<Categoria>>() {
@Override
public void onResponse(Call<List<Categoria>> call, Response<List<Categoria>> response) {
int code = response.code();
if (code == 200) {
List<Categoria> lista = response.body();
for (Categoria categoria : lista) {
Log.d("MainActivity", categoria.descricao);
}
}
else {
Toast.makeText(getBaseContext(), "Falha: " + String.valueOf(code),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<List<Categoria>> call, Throwable t) {
}
});
}
But when I do this nothing happens, I do not see error message or showing the descriptions in Log. d.
However, the following information is uploaded
08-21 14:41:59.637 6491-6491/dev.sbruno.sibcom I/Choreographer: Skipped 36 frames! The application may be Doing Too Much work on its main thread. 08-21 14:42:04.622 6491-6491/dev.sbruno.sibcom D/Networksecurityconfig: No Network Security Config specified, using Platform default 08-21 14:42:17.388 6491-6496/dev.sbruno.sibcom I/art: Do partial code cache Collection, code=30KB, data=28KB 08-21 14:42:17.391 6491-6496/dev.sbruno.sibcom I/art: After code cache Collection, code=23KB, date=25KB 08-21 14:42:17.391 6491-6496/dev.sbruno.sibcom I/art: Increasing code cache Capacity to 128KB
What is going wrong? even trying to debug can’t do as it is not possible to access the lines I put to debug, it doesn’t even reach them
Edit
I just added a Toast on onFailure and checked that the code is going through there, so there must be some error in the connection is this?
@Override
public void onFailure(Call<List<Categoria>> call, Throwable t) {
Toast.makeText(getBaseContext(), "Falhou ao me conectar ",
Toast.LENGTH_LONG).show();
What is the message within t (Throwable)?
– Leonardo Lima
The t message is java.net.Sockettimeoutexception: connect timed out ... Detailmessage: "connect timed out", that’s what I wanted to know or want to see all the values that t returns?
– Bruno Aparecido da Silva
Your app may not be able to connect to the host. You can query this API in other ways (Curl, Postman, etc)?
– Leonardo Lima
Yes Leonardo, through Postman it loads all the json when I put the link where it is http://localhost/sibcomslim/category
– Bruno Aparecido da Silva