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() {
Map<String, String> params = new HashMap<String, String>();
params.put(Server.KEY_USERNAME, username);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(strReq);
}
source
share