I'm creating my first Android APP but I'm fighting some problems that I actually don't understand how to solve.
My app have a point system and every day I give some points to the users by just doing a Login and clicking a button. But aparently I'm having an issue because the value of the response is only updating when I log out and log in again or when I uninstall and install the app again, if I close and then re-open the app , the value is still the same.
For example, if I'm opening the app for the first time, clicking the button it will say "Here are your 10 points for Login Daily", then I click the button again and the app will say "Here are your 10 points for Login Daily", but the weird part here is that the server don't add the points to the user the second time, so either the response returns a wrong state or the android app is showing stored weird data. Then if you Re-Login or Re-Install the next time that you click it will say "You have to wait X Hours"
Here is my code,
Android Request:
private void dailyCheckin(final ProgressDialog pDialog) {
//first getting the values
final String reg_user_id=String.valueOf(SharedPref.getInstance(getApplicationContext()).getUserID());
//Call our volley library
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.DAILYCHECKIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
if (obj.getBoolean("error")) {
pDialog.hide();
Toast.makeText(getApplicationContext(),
getString(R.string.newdailyreward)
+" "+obj.getString("hours")+" "+
getString(R.string.hours)
+" "+obj.getString("minutes")+" "+
getString(R.string.minutes), Toast.LENGTH_SHORT).show();
} else {
pDialog.hide();
Toast.makeText(getApplicationContext(),
getString(R.string.rewardsuccess)
+" "+obj.getString("message")+" "+
getString(R.string.rewardpoints)
, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
pDialog.hide();
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.hide();
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("user_id", reg_user_id);
return params;
}
};
VolleySingleton.getInstance(FreePointsActivity.this).addToRequestQueue(stringRequest);
}//dailyCheckin
PhP Response Function:
elseif (isset($_GET['dailycheckin'])) {
$id=intval($_POST['user_id']);
$daily="Daily Checkin";
$sql='select * from rewardpoints_points where user_id='.$id.' AND app_name="'.$daily.'" ORDER BY date_operation DESC;';
$res=$cnx->query($sql);
$data=$res->fetchAll(PDO::FETCH_OBJ)[0];
if (empty($data)) {
$s='select daily_chekin_points from rewardpoints_settings';
$r=$cnx->query($s);
$d=$r->fetchAll(PDO::FETCH_OBJ)[0];
$points=$d->daily_chekin_points;
$sql="insert into rewardpoints_points values(NULL,'".$id."',0,'".$daily."','".$points."',NOW(),0)";
if($cnx->exec($sql)){
$response['error'] = false;
$response['message'] = $points;
}
}
else{
$newreward= strtotime( $data->date_operation ) + 24 * 3600;
if ($newreward<=time()) {
//echo "you can get";
$s='select daily_chekin_points from rewardpoints_settings';
$r=$cnx->query($s);
$d=$r->fetchAll(PDO::FETCH_OBJ)[0];
$points=$d->daily_chekin_points;
$sql="insert into rewardpoints_points values(NULL,'".$id."',0,'".$daily."','".$points."',NOW(),0)";
if($cnx->exec($sql)){
$response['error'] = false;
$response['message'] = $points;
}
}//$newreward<=time()
else{
//echo "you can not";
$response['error'] = true;
$time=time()-$newreward;
$m = floor($time / 60)*-1;
$hours = floor($m / 60);
$minutes = ($m % 60);
$response['hours'] = $hours;
$response['minutes'] = $minutes;
}
}
}//dailycheckin