Volley: onErrorResponse is returning the error as null

Asked

Viewed 69 times

0

My server is registering normally, but when trying to send a record to register using Volley, it drops to onErrorResponse and the object error.getMessage() is returning null.

My java code:

    private String url = "http://10.0.2.2:8080/structure/insert_montadora.php/";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_cmontadora, container, false);

        edtDesc = view.findViewById(R.id.editTextDescricao);
        btnSalvar = view.findViewById(R.id.buttonSalvar);

        requestQueue = Volley.newRequestQueue(mContext);

        btnSalvar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean campovalidado = false;

                if (edtDesc.getText().length() == 0){
                    Toast.makeText(getContext(), mContext.getResources().getString(R.string.msg_0022), Toast.LENGTH_SHORT).show();
                } else
                    campovalidado = true;

                if (campovalidado) {
                    try {
                        stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Log.i("INFO", response);
                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Log.e("INFO", "Erro: " + error.getMessage());
                            }
                        }) {
                            @Override
                            protected Map<String, String> getParams() throws AuthFailureError {
                                super.getParams();
                                Map<String, String> params = new HashMap<>();
                                params.put("descricao", edtDesc.getText().toString());
                                return params;
                            }

                            @Override
                            public Map<String, String> getHeaders() throws AuthFailureError {
                                super.getHeaders();
                                HashMap<String, String> headers = new HashMap<>();
                                headers.put("Content-Type", "application/json; charset=utf-8");
                                return headers;
                            }
                        };
                        stringRequest.setRetryPolicy(new DefaultRetryPolicy(1000*5, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                        requestQueue.add(stringRequest);
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });
        return view;
    }

My connection to server:

<?php
header('Content-Type: application/json; charset=utf-8');

    include 'db_config.php';

    $response = array();
    $response['erro'] = true;

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){


        $conn = new mysqli($DB_SERVER, $DB_USER, $DB_PASSWORD, $DB_DATABASE);

        mysqli_set_charset($conn, "utf8");

        if ($conn-> connect_error){
            die ("Ops, falhou...: " . $conn->connect_error);

        } else {
            echo ("Conectado");
        }

        $descricao = $_POST['descricao'];

        $sql = ("INSERT INTO tb_montadora (id, descricao) VALUES ('', '".$descricao."')");


        if (mysqli_query($conn, $sql)) {
            $response['erro'] = false;
            $response['mensagem'] = "Cadastrado com sucesso";
        } else {
            echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            $response['mensagem'] = "Falha ao cadastrar";
        }

        $conn->close();
    }

    echo json_encode($response);
?>
  • Your url is not defined in Volley. It is an attribute of Fragment?

  • Yes, I set it right in the "header"

  • What error shows in logs?

  • No error, just displays my log that is in onErrorResponse as null 03-01 11:52:50.047 596-702/? D/Sensorservice: 0.0 0.3 9.6 03-01 11:52:50.217 767-767/? D/STATUSBAR-Networkcontroller: refreshSignalCluster: data=0 Bt=false 03-01 11:52:50.578 25869-25869/com.example.chiptronic_371.testescampo E/INFO: Error: null

  • Look, you’re launching in Header (header) that the information sent is a json. However, you send a string. Remove the headers.put("Content-Type", "application/json; charset=utf-8"); and see if the information is sent.

  • It seems to me that the information is not even sent to the URL, so the answer is null.

  • Hello, a friend of service helped me solve, it was the path where the url was searching, as I am simulating on a mobile phone and the server is local on the machine, I had to pass the ip of the machine and the url was like this: Private string url = "http://192.168.110.108:8080/Structure/insert_montadora.php";. It is good to note also that I was putting a / at the end of the url, which is not necessary.

  • Thank you very much Andrei Coelho for your attention!

  • True! That’s why there was no answer. = ) Quiet

Show 4 more comments
No answers

Browser other questions tagged

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