I have a Score.php file to update to a field called score of integer type in a mysql database:
$con = mysqli_connect("localhost", "root", "", "basededatos");
$user_id = $_POST["id_user"];
$username = $_POST["username"];
$score= $_POST["score"];
$statement = mysqli_prepare($con, "UPDATE user SET score= ? WHERE id_user=? AND username=?");
mysqli_stmt_bind_param($statement, "isi", $id_user, $username, $score);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $user_id, $name, $username, $password, $date, $email, $score);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement))
{
$response["success"] = true;
$response["id_user"] = $user_id;
$response["name"] = $name;
$response["username"] = $username;
$response["password"] = $password;
$response["date"] = $date;
$response["email"] = $email;
$response["score"] = $score;
}
echo json_encode($response);
The class in android studio that calls that php file is:
public class ScoreRequest extends StringRequest
{
private static final String Score_Request_URL="http://192.168.1.221/Score.php";//declaramos una constante
private Map<String, String> params;
//constructor de la clase
public ScoreRequest (int userid, String username, int scoreaux, Response.Listener<String> listener)
{
super(Request.Method.POST,Score_Request_URL,listener, null);
params= new HashMap<>();
params.put("id_user", userid+"");
params.put("username", username);
params.put("score", scoreaux+"");
}//fin de constructor
I also have the activity.java where I need that when I press a button called btnno I do the update in the database of the field called score, and at the same time it leads me to another activity2 with the updated score field since I add 1 .
The code of the Button method (btnno) in activity is:
btnno.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
int scoreaux;
scoreaux=Integer.parseInt(score);
scoreaux+=1;
final String usernameaux=username;
final int useraux=Integer.parseInt(userid);
Response.Listener<String> responseListener= new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
try
{
JSONObject jsonResponse=new JSONObject(response);
boolean success= jsonResponse.getBoolean("success");
if(success)
{
int user=jsonResponse.getInt("id_user");
String nameuser=jsonResponse.getString("username");
int scoreb=jsonResponse.getInt("score");
Intent intent=new Intent (Digits.this, Digits2.class);
intent.putExtra("id_user", user);
intent.putExtra("username", nameuser);
intent.putExtra("score", scoreb);
Digits.this.startActivity(intent);
}
else
{
AlertDialog.Builder builder=new AlertDialog.Builder(Digits.this);
builder.setMessage("User or Id incorrect")
.setNegativeButton("Retry", null)
.create().show();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}//fin de método onResponse
};//fin de Response.Listener<String>
ScoreRequest scoreRequest= new ScoreRequest(useraux,usernameaux,scoreaux, responseListener);
RequestQueue queue= Volley.newRequestQueue(Digits.this);
queue.add(scoreRequest);
}//fin de onclick
});//fin de setOnClickListener
//fin de método btnno
and the code for activity2 is:
public class Digits2 extends AppCompatActivity
{
String username;
int userid, score;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_digits2);
Intent intent =getIntent();
score=intent.getIntExtra("score",-1);
username=intent.getStringExtra("username");
userid=intent.getIntExtra("id_user",-1);
}
}
I made debug and this throws the logcat:
12-19 19: 02: 37.088 541-634 / system_process W / AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client 12-19 19: 02: 37.279 2327-2327 / matgic.com.matgic W / System.err: org.json.JSONException: Value (JSONObject.java:160) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at org.json.JSONObject. (JSONObject.java:173) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at matgic.com.matgic.Digits $ 1 $ 1.onResponse (Digits.java:123) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at matgic.com.matgic.Digits $ 1 $ 1.onResponse (Digits.java:117) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at com.android.volley.toolbox.StringRequest.deliverResponse (StringRequest.java:60) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at com.android.volley.toolbox.StringRequest.deliverResponse (StringRequest.java30) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run (ExecutorDelivery.java:99) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at android.os.Handler.handleCallback (Handler.java:739) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at android.os.Handler.dispatchMessage (Handler.java:95) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at android.os.Looper.loop (Looper.java:135) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at android.app.ActivityThread.main (ActivityThread.java:5221) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at java.lang.reflect.Method.invoke (Native Method) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at java.lang.reflect.Method.invoke (Method.java:372) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:899) 12-19 19: 02: 37.280 2327-2327 / matgic.com.matgic W / System.err: at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:694)
The problem is that it does not lead me to the activity2 In advance, thanks for your valuable time