Reputation: 127
I am trying to send data from android to php using POST method
the php part is tested alone and it's 100% working and it returns a json string as supposed
the android part isn't working it doesn't print error or even the required response
Here is the code and it's tested , printing a string "test" using Toast , but it doesn't change in the volley code :(
this is the 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 android code:
initializing the test string to display output
String test = "none";
this is 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);
}
Upvotes: 4
Views: 4828
Reputation: 1291
Check if u have taken internet permission .
Also add toast in
onResponse()
Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_LONG).show();
Upvotes: 1
Reputation: 7683
You have a concurrency issue. When you execute your setUserData(username)
method Volley is doing the work on a background thread. Therefore you need to wait for the response before you can display it in a Toast. At the moment when you call:
Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();
The server still hasn't responded and test
will still be none
. You need to call the toast from the onResponse
callback:
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();
}
Upvotes: 1