Galera, estou tentando consumir uma api rest com o retrofit mais nunca retorna nada:
Preciso pegar esse json simples nessa url:
http://192.168.0.5:8080/vt-services/usuarios/login/usuario/usuario/senha/123
{“id”:1,“usuario”:“usuario”,“senha”:“123”,“mensagem”:null}
Minha Interface:
public interface UserService {
@GET("/usuarios/login/usuario/{usuario}/senha/{senha}")
public Call<UserRest> login(@Path("usuario") String usuario, @Path("senha") String senha);
}
Criando Deserializer
public class UserDeserializer implements JsonDeserializer<Object> {
@Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonElement u = json.getAsJsonObject();
UserRest userRest = new Gson().fromJson(u, UserRest.class);
return userRest;
}
}
Chamando o service
public class UsuarioRetrofit {
private UserRest userRest = null;
public UserRest login(Usuario usuario){
try {
// UserService service = ServiceGenerator.createService(UserService.class);
Gson gson = new GsonBuilder().registerTypeAdapter(UserRest.class,new UserDeserializer()).create();
Retrofit retrofit = new Retrofit.
Builder().
baseUrl("http://192.168.0.5:8080/vt-services").
addConverterFactory(GsonConverterFactory.create(gson)).
build();
UserService service = retrofit.create(UserService.class);
Call<UserRest> call = service.login(usuario.getEmail(),usuario.getSenha());
call.enqueue(new Callback<UserRest>() {
@Override
public void onResponse(Response<UserRest> response, Retrofit retrofit) {
Log.d("vamostrocar",response.toString());
userRest = response.body();
}
@Override
public void onFailure(Throwable t) {
Log.d("vamostrocar",t.getMessage());
}
});
} catch (Exception e) {
Log.e("vamostrocar", e.getMessage());
}
return userRest;
}
}
Estranho que nunca cai no onResponse ou no onFailure, se faço a mesma chamada com AysncTask funciona corretamente, a permissao de internet esta configurada, faço simplesmente a chamada em uma activity
@OnClick(R.id.entrar)
public void login(View v) {
Usuario usuario = new LoginHelper(this).pegaUsuarioFormulario();
try{
UsuarioDAO usuarioDAO = new UsuarioDAO(this);
final boolean existeUsuario = usuarioDAO.verificaLogin(usuario);
usuarioDAO.close();
if(existeUsuario){
//final UserRest userRest = new UsuarioAsyncTask(this, usuario).execute().get();
final UserRest userRest = new UsuarioRetrofit().login(usuario);
startActivity(new Intent(this, MenuActivity.class));
}else{
Toast.makeText(this, "Usuario nao encontrado", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Log.e("vamostrocar",e.getMessage());
}
}`