dqm4675 2018-10-12 18:36
浏览 181

使用PHP从Android Studio向mySQL数据库发送值

I'm running an app quiz with 2 table like this https://gyazo.com/641f6e45006ab4f6d962a81d60e1ba69 , https://gyazo.com/be10c2f550f63b4ebc55eda61c3f0f15. My register -login activity works fine but the issue is when I try to send the score value to database. No errors and no message that's running. Can you help me please?

public class Results extends AppCompatActivity implements OnClickListener {
private Button bt_save;
private TextView tv_label;

int totalscore ;
private ProgressDialog progressDialog;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
    tv_label = (TextView) findViewById(R.id.tv_label);
    Button bt_Begin = (Button) findViewById(R.id.bt_Begin);
    bt_save = (Button)findViewById(R.id.bt_save);
    bt_save.setOnClickListener(this);




    SharedPreferences sharedPreferences = getSharedPreferences("sharedPref",Context.MODE_PRIVATE);
    Integer result = sharedPreferences.getInt("rightAnswerCount", 0);

    SharedPreferences sharedPreferencess = getSharedPreferences("sharedPref",Context.MODE_PRIVATE);
    Integer results = sharedPreferencess.getInt("rightAnswerCountt",0);
         totalscore=(result+results);
         tv_label.setText("" +totalscore);



}


private void saveRequest() {
  final  String score = tv_label.getText().toString().trim();



    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Registrating user...");
    progressDialog.show();



    StringRequest stringRequest = new StringRequest(Request.Method.POST,
            Constants.RESULT_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        Toast.makeText(getApplicationContext(), jsonObject.getString("message"), Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.hide();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> key = new HashMap<>();
            key.put("totalscore", score+"");

            return key;
        }

    };
    RequestHandler.getInstance(this).addToRequestQueue(stringRequest);



  }


@Override
public void onClick(View view) {
    if (view == bt_save)
        saveRequest();
}

this is my php file dbOp1

<?php
class dbOperation1{
private $con;
function __construct(){
require_once dirname(__FILE__).'/connect.php';

$db = new connect();
$this->con = $db->connect();
}
public function saveScore($score){


$stmt = $this->con->prepare("INSERT INTO `test_score`(`id`,`score`) VALUES(NULL, ?);");            




$stmt->bind_param("i",$score);
if($stmt->execute()){
 return 1;
}else{
 return 2;
     }
 }
 }     

 ?>

And this is my Result php:

 <?php
error_reporting(0);
require_once 'dbOp1.php';

$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){

if(

isset($_POST['score']))
{

    $db = new dbOperation1();
    $result = $db->saveRequest( 
                               $_POST['score']



                                   );   

    if($result == 1){
        $response['error'] = false;
        $response['message'] = "User registered sussessfully";
        }elseif($result == 2){
        $respone['error'] = true;
        $response['message'] = "Some error";
        }
     }
     }

    echo json_encode($response);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog