1

I'm trying to make a POST gson request... I need to pass a body as an array of bytes...

In my textview into the final of the array, it was showing /u0000... And when it call the server, the server send me 400...

JSONObject jsonObjectUsuario = new JSONObject();

try {
    jsonObjectUsuario.put("nome", et_nome.getText().toString());
    jsonObjectUsuario.put("cpf", et_cpf.getText().toString());
    jsonObjectUsuario.put("email", et_email.getText().toString());
    jsonObjectUsuario.put("senha", et_password.getText().toString());
} catch (JSONException e) {
    e.printStackTrace();
}

UserRequestHelper.userRequest(
    Request.Method.POST,
    EndpointURL.POST_USUARIO,
    jsonObjectUsuario.toString().getBytes(), 
    new Response.Listener<Usuario>() {
        @Override
        public void onResponse(Usuario response) {
            Toast.makeText(
                getActivity(),
                "Cadastro realizado com sucesso!",
                Toast.LENGTH_SHORT).show();

            getActivity()
                .getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_initial, new LoginFragment())
                .remove(new UsuarioFragment())
                .commit();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(
                getActivity(),
                "Erro ao realizar cadastro.", 
                Toast.LENGTH_SHORT)
           .show();
        }
    }
);

The Request

    public GsonRequest(int method, String url, Class<T> clazz, byte[] body,
                   Response.Listener<T> listener, Response.ErrorListener errorListener) {
    super(method, url, errorListener);
    this.clazz = clazz;
    this.body = body;
    this.listener = listener;
}

User Request Code...

    public static void userRequest(int method, String url, byte[] body, Response.Listener<Usuario> listener, Response.ErrorListener errorListener){
    GsonRequest<Usuario> usuarioGsonRequest = new GsonRequest<>(method, url, Usuario.class, body, listener, errorListener);

    VolleySingleton.getsInstance().addToRequestQueue(usuarioGsonRequest);
}

The GsonRequest body...

@Override
public byte[] getBody() throws AuthFailureError {
    return body != null ? body : super.getBody();
}

What's wrong? Someone have an Gson POST request example for me?

Please, help me!!!

Community
  • 1
  • 1
Felipe A.
  • 929
  • 3
  • 12
  • 28

0 Answers0