retrofit 2 error malformed when sending a post

Asked

Viewed 21 times

0

When I send the data via POST I return that JSON is wrong in the formation. Follow the script button

 Button btnretrofit =(Button)findViewById(R.id.btnretofit);
    btnretrofit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String nome = Nome.getText().toString();
            String dataNascimento = DataNascimento.getText().toString();
            String cell = Cell.getText().toString();
            String Email = email.getText().toString();
            String Sobrenome = sobrenome.getText().toString();


            APIServices apiservice = retrofit.create(APIServices.class);

            Call<User> call = apiservice.createUser(nome,dataNascimento,cell,Email,Sobrenome);

            call.enqueue(new Callback<User>() {
                @Override
                public void onResponse(Call<User> call, Response<User> response) {

                    if(response.isSuccessful()){
                        Toast.makeText(getApplicationContext(),"sucesso"+ response.code(), Toast.LENGTH_LONG).show();
                    }else{

                        Toast.makeText(getApplicationContext(),"Erro a conectar ao servidot"+ response.code(), Toast.LENGTH_LONG).show();
                    }

                }

                @Override
                public void onFailure(Call<User> call, Throwable t) {

                    Toast.makeText(getApplicationContext(),"Erro a conectar ao servidot"+ t.getMessage(), Toast.LENGTH_LONG).show();

                }
            });

        }
    });

In my api follows the code

@FormUrlEncoded
@POST("/gson/insertData.php")
Call<User>  createUser(
        @Field("Nome")String nome,
        @Field("DataNascimento") String dataNascimento,
        @Field("Cell") String cell,
        @Field("email") String Email,
        @Field("Sobrenome") String Sobrenome
);

In my class

public class User {
    private Integer IdUsuario;

    private String nome;
    private String cell;
    private String DataNascimento;
    private String email;
    private String Sobrenome;


    public User( String nome, String dataNascimento, String cell,  String email, String Sobrenome ) {
        this.nome = nome;
        this.cell = cell;
        this.DataNascimento = dataNascimento;
        this.email = email;
        this.Sobrenome = Sobrenome;
    }

    public Integer getIUsuario() {
        return IdUsuario;
    }
}
  • I performed the correction of the posted difficulty with the line . setLenient() Gson gson = new Gsonbuilder() . setDateFormat("yyyy-MM-dd’T'HH:mm:ssZ") . setLenient() . create();

1 answer

0

Staff the resolution of the posted difficulty, was to insert in the code below the line . setLenient()

Gson gson = new Gsonbuilder() . setDateFormat("yyyy-MM-dd’T'HH:mm:ssZ") . setLenient() create. create();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.