Android: Volley onresponse string request not called

I am trying to send data from android to php using the POST method

the php part is tested on its own and it works 100% and returns the json string as intended

the android part does not work, it does not print an error or even the required answer

Here is the code and it is tested by printing the string "test" with Toast, but it does not change in the volleyball code :(

this is php code:

<?php
    require_once(__DIR__.'/../dbConnect.php');  


    $userName=$_POST['username']; 

    $query = "SELECT * FROM users WHERE username='$userName'";
    $result = pg_query($con, $query);
    $rows = pg_num_rows($result);

    while ($row = pg_fetch_row($result)) 
    {
        $field = pg_num_fields( $result );

        for ( $i = 0; $i < $field; $i++ ) {

            $name = pg_field_name( $result, $i );
            $user_arr[$name] = $row[$i]; 
        }               
    }

    echo json_encode($user_arr);

    pg_close($con); 

    ?>

and this is the android code:

initialization of a test line for output

String test = "none";

that's where i call the method

  setUserData(username);
  Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();

and this is the method:

  private void setUserData(final String username)
    {
        StringRequest strReq = new StringRequest(Request.Method.POST,
                Server.GET_USERDATA_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                   test= "test = "+response;

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();

            }
        })

        {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to getData url
                Map<String, String> params = new HashMap<String, String>();
                params.put(Server.KEY_USERNAME, username);
                return params;
            }

        };
        //Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //Adding request to the queue
        requestQueue.add(strReq);
    }
+4
source share
2 answers

concurrency. setUserData(username), Volley . , Toast. , :

Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();

, test - none. onResponse:

private void setUserData(final String username) {

    StringRequest strReq = new StringRequest(
        Request.Method.POST,
        Server.GET_USERDATA_URL, 
        new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                test = "test = "+response;
                showToast();

            }
        }, 
        new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();

        }
    })

    {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to getData url
            Map<String, String> params = new HashMap<String, String>();
            params.put(Server.KEY_USERNAME, username);
            return params;
        }

    };
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    //Adding request to the queue
    requestQueue.add(strReq);
}

private void showToast() {

    Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();
}
+1

, -.

onResponse()
 Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_LONG).show(); 
+1

All Articles