简体   繁体   中英

Getting onFailure Response in Callback Android Studio

I'm trying to get a List from my server as I've done in other classes and it works. But now I'm getting an error in this Callback. Always goes to onFailure I test with postman and the server returns the list well.

         RetrofitOwn retro = new RetrofitOwn();
        Retrofit retrofit = retro.getObjectRetrofit();

        GitHubClient etakemonsusuario = retrofit.create(GitHubClient.class);

        Call<List<Captura>> call = etakemonsusuario.getCapturasUsuario(idloged);

        call.enqueue(new Callback<List<Captura>>() {
            @Override
            public void onResponse(Call<List<Captura>> call, Response<List<Captura>> response) {
                if (response.isSuccessful()){

                    listView = (ListView) findViewById(R.id.listEtakemons);
                    listarecibida = (List<Captura>) response.body();
                    listacapturas = new ArrayList<String>();

                    for (int i = 0; i < listarecibida.size(); i++) {
                        Captura captura = listarecibida.get(i);
                        listacapturas.add(captura.getNombreetakemon());
                    }

                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>
                            (EtakemonsUsuario.this, android.R.layout.simple_list_item_1, listacapturas);
                    listView.setAdapter(arrayAdapter);
                }
                else{
                    Toast.makeText(EtakemonsUsuario.this, "Peticion erronea!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<Captura>> call, Throwable t) {
                Toast.makeText(EtakemonsUsuario.this, "No hay conexión!", Toast.LENGTH_SHORT).show();
                 Log.d(tag, "ERROR al conectar!");
            }
        });

The API call in the interface is declared as following:

@GET ("usuario/{id}/get_capturas")
        Call<List<Captura>> getCapturasUsuario(@Path("id") int id);

Also the code from the server Controller is:

        @GET
    @Path("/{id}/get_capturas")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getCapturasUsuario(@PathParam("id") int idUsuario){

        Usuario usuario = new Usuario();
        if (idUsuario != 0){
            Usuario usuario2 = new Usuario();
            List<Captura> capturaList =  usuario2.getCapturasUsuario(idUsuario);
//            for (int i = 0; i < capturaList.size(); i++){
//                System.out.println(capturaList.get(i).getNombreetakemon());
//            }
            GenericEntity<List<Captura>> entity = new GenericEntity<List<Captura>>(capturaList) {};
            return Response.status(201).entity(entity).build();
        }
        else
        {
            String noResult = "No tiene capturas.";
            return Response.status(404).entity(noResult).build();
        }
    }

And the function the controller calls is:

public List<Captura> getCapturasUsuario(int id) {
List<Captura> listaCapturaUsuario = new ArrayList<Captura>();
try {
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Captura WHERE captura.idusuariosss = " + id);
    while (rs.next()) {
        Captura capturaUsuario = new Captura();
        capturaUsuario.setId(rs.getInt("id"));
        capturaUsuario.setIdusuariosss(rs.getInt("idusuariosss"));
        capturaUsuario.setIdetakemon(rs.getInt("idetakemon"));
        capturaUsuario.setNivel(rs.getInt("nivel"));
        capturaUsuario.setExperiencia(rs.getInt("experiencia"));
        capturaUsuario.setVida(rs.getInt("vida"));
        capturaUsuario.setAtaque(rs.getInt("ataque"));
        capturaUsuario.setDefensa(rs.getInt("defensa"));
        capturaUsuario.setEstado(rs.getInt("estado"));
        capturaUsuario.setFecha(rs.getDate("fecha"));
        capturaUsuario.setNombreetakemon(rs.getString("nombreetakemon"));
        capturaUsuario.setHabilidadetakemon(rs.getString("habilidadetakemon"));
        capturaUsuario.setTipoetakemon(rs.getInt("tipoetakemon"));
        capturaUsuario.setImagen(rs.getString("imagen"));
        listaCapturaUsuario.add(capturaUsuario);
    }
} catch (SQLException e) {
    e.printStackTrace();
    logger.error("getCapturasUsuario: "+e.getMessage());
}

return listaCapturaUsuario;

}

Anyone knows where could be the problem?

I thought the response from the server is successful but android studio says it's unsuccessful

Thanks a lot!

I've just found this error

You need to define a Date format to parse the date. In your retrofit instance use a gson builder. Try this:

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
}

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM