我正在尝试将android应用程序中的mysql表中的值增加1。一旦成功,我正在尝试推出一项新活动。我在下面包含了android和php代码。单击按钮时DB正确地更新,但我无法弄清楚新活动未启动的原因。我确定它的东西很小但我无法弄清楚,任何帮助都会非常感激。我已将新活动包含在清单中。
Android - MainActivity
package com.example.conn.updatescore;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private Button updateScore;
private RequestQueue requestQueue;
private static final String URL = "http://192.168.0.14/updatemysql/update_control.php";
private StringRequest request;
public static final String teamName = "Justice league";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updateScore = (Button) findViewById(R.id.updateScore);
requestQueue = Volley.newRequestQueue(this);
updateScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("success")) {
Toast.makeText(getApplicationContext(), "SUCCESS " + jsonObject.getString("success"), Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), SuccessActivity.class));
} else {
Toast.makeText(getApplicationContext(), "Error" + jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("name", teamName.toString());
return hashMap;
}
};
requestQueue.add(request);
}
});
}
}
SuccessActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by conn on 09/08/2017.
*/
public class SuccessActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
}
}
update_control.php
class Update {
private $db;
private $connection;
function __construct(){
$this->db = new DB_Connection();
$this->connection = $this->db->get_connection();
}
public function update_score($name){
$query = " UPDATE `team` SET `member` = member+1 WHERE name = '$name'";
$is_inserted = mysqli_query($this->connection, $query);
if ($is_inserted ==1){
$json['success'] = 'score updated' .$name;
}else {
$json['error'] = 'failed';
}
echo json_encode($json);
mysqli_close($this->connection);
}
}
$update = new Update();
if (isset($_POST['name'])){
$name = $_POST['name'];
if (!empty($name)){
$update->update_score($name);
} else{
echo ("name not set");
}
}