What fails when sending data with retrofit to the api in SLIM?

0

Hi, I am working with retrofit 2.3.0 and slim v2, and when sending the data by means of retrofit, the json that has to be received is not returned and it may not even send the data of the form, since another function of the api if it works for me

endpoints file (interface retrofit)

public interface apiAdapter {


    @POST("login/{usuario}/{password}/{googleId}/{idDispositivo}")
    Call<resultado> login(@Path("usuario") String usuario,@Path("password") String password,@Path("googleId") String googleId,@Path("idDispositivo") String idDispositivo);

    @POST("guardarForm/{ubicacion}/{contrato}/{inicial}/{final}/{total}/{bobina}/{usuario}/{servicio}/{orden}")
    Call<resultado> guardarServicioForm (@Path("ubicacion") String ubicacion,@Path("contrato") String contrato,@Path("inicial") String inicial,@Path("final") String finl,@Path("total") int total,@Path("bobina") String bobina,@Path("usuario") int usuario, @Path("servicio") int servicio,@Path("orden") int orden);




    @GET("login2")
    Call<resultado> mensaje();




}

this the file that manages the json that receives

public class resultado {
    @SerializedName("estatus")
    @Expose
    private String estatus;

    public String getEstatus() {
        return estatus;
    }

    public void setEstatus(String estatus) {
        this.estatus = estatus;
    }
}

and the following code is the part of the function where I click to send the form

Retrofit retrofit2 = new ApiConfigRetrofit().consultas();
            apiAdapter servicio = retrofit2.create(apiAdapter.class);
            //persona pepe = new persona(email,password,"dfgf","dfgfd");
            Call<resultado> llamada = servicio.login("sdfs","password","fdgf","dfdgf");
            llamada.enqueue(new Callback<resultado>() {
                @Override
                public void onResponse(Call<resultado> call, Response<resultado> response) {

                      Toast.makeText(getApplicationContext(),response.body().getEstatus(),Toast.LENGTH_SHORT).show();



                }

                @Override
                public void onFailure(Call<resultado> call, Throwable t) {
                    Toast.makeText(getApplicationContext(),t.toString(),Toast.LENGTH_SHORT).show();
                }
            });

and this is the error

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: appstda.erp.com.mx.siga40, PID: 10401
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String pruebasEjemplos.resultado.getEstatus()' on a null object reference
                                                                               at LoginActivity$4.onResponse(LoginActivity.java:237)
                                                                               at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                                                                               at android.os.Handler.handleCallback(Handler.java:815)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                               at android.os.Looper.loop(Looper.java:194)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5631)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
    
asked by JesusSh 09.09.2017 в 00:03
source

1 answer

0

What happens in your case is that you are receiving a response from the server but it is not what you expect, the body of the response is null in the next line

                  Toast.makeText(getApplicationContext(),response.body().getEstatus(),Toast.LENGTH_SHORT).show();

When you enter the onResponse method of your retrofit it means that the connection to the web service was successful, however it may happen that it does not return the object you expect and instead it returns a code with the error and a message , to avoid that your application is broken and to see what is happening you should the next change

Retrofit retrofit2 = new ApiConfigRetrofit().consultas();
            apiAdapter servicio = retrofit2.create(apiAdapter.class);
            //persona pepe = new persona(email,password,"dfgf","dfgfd");
            Call<resultado> llamada = servicio.login("sdfs","password","fdgf","dfdgf");
            llamada.enqueue(new Callback<resultado>() {
                @Override
                public void onResponse(Call<resultado> call, Response<resultado> response) {

                    if (response.body() != null) {
                      Toast.makeText(getApplicationContext(),response.body().getEstatus(),Toast.LENGTH_SHORT).show();
                    } else if(response.code() != null) {
                        Log.d("Error", String.valueOf(response.code()) + response.message());
                    }

                }

                @Override
                public void onFailure(Call<resultado> call, Throwable t) {
                    Toast.makeText(getApplicationContext(),t.toString(),Toast.LENGTH_SHORT).show();
                }
            });

Remember to check the android studio log so you know what the error is when sending the data, maybe your model "result" differs from what you expect in the web service (the name of the attribute for example).

    
answered by 10.09.2017 / 01:52
source