为什么我的代码无法连接到我的MySQL数据库?

时间:2018-09-19 15:23:12

标签: android mysql json laravel-5

我有一个带有laravel后端的移动应用程序,我想向我的数据库添加一个客户端。每个客户端都有很多信息,我试图在两个活动中削减表单,但是我的代码没有添加任何内容,并且向我显示了一个小错误(json.error)。 有人可以帮我,这是我的第一次活动

 btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String uName = nom.getText().toString().trim();
           String mail  = email.getText().toString().trim();
           String pass  = password.getText().toString().trim();
           String _preno  = prenom.getText().toString().trim();
           String _tel  = tel.getText().toString().trim();
           String _mobile  = mobile.getText().toString().trim();
            Intent intent = new Intent(SignUp.this, adresse.class);
            intent.putExtra("nom", uName);
            intent.putExtra("prenom", _preno);
            intent.putExtra("password", pass);
            intent.putExtra("tel", _tel);
            intent.putExtra("mobile", _mobile);
            intent.putExtra("email", mail);
            startActivity(intent);
        }
    });

这是我的第二项活动

 readData(getIntent());
       signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signUp();
        }
    });
}


private void signUp() {
    gpsTracker = new GpsTracker(adresse.this);
    //   insert();
    if (gpsTracker.canGetLocation()) {
        latitude = gpsTracker.getLatitude();
        longitude = gpsTracker.getLongitude();
        //  tvLatitude.setText(String.valueOf(latitude));
        //   tvLongitude.setText(String.valueOf(longitude));

    } else {
        gpsTracker.showSettingsAlert();
    }
    RequestQueue queue = Volley.newRequestQueue(adresse.this);
    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.POST, 
         Utils.REGISTER_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("adresse", "Register Response: " + response);

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {
                    JSONObject user = jObj.getJSONObject("user");
                    String uName = user.getString("nom");
                    String email = user.getString("email");
                    String prenom = user.getString("prenom");
                    String mobile = user.getString("mobile");
                    String tel = user.getString("tel");
                    String ville = user.getString("ville");
                    String rue = user.getString("rue");
                    String code_postal = user.getString("code_postal");
                    String latitude = user.getString("latitude");
                    String longitude = user.getString("longitude");
                    // Inserting row in users table
                    userInfo.setEmail(email);
                    userInfo.setUsername(uName);
                    userInfo.setPrenom(prenom);
                    session.setLoggedin(true);

                    startActivity(new Intent(adresse.this, 
       MainActivity.class));
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error_msg");
                    toast(errorMsg);
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                toast("Json error: " + e.getMessage());
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Login Error: " + error.getMessage());
            toast("Unknown Error occurred");
            progressDialog.hide();
        }
    }) {
        //adding parameters to the request
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("ville", ville.getText().toString());
            params.put("rue", rue.getText().toString());
            params.put("code_postal", code_postal.getText().toString());
            params.put("latitude", latitude.toString());
            params.put("longitude", longitude.toString());
            params.put("nom", uname);
            params.put("prenom", prenom);
            params.put("email", email);
            params.put("password", password);
            params.put("tel", tel);
            params.put("mobile", mobile);
            return params;
        }
    };
    // Add the request to the RequestQueue.
    queue.add(stringRequest);

    }

      private void toast(String x){
    Toast.makeText(this, x, Toast.LENGTH_SHORT).show();
}

   private void readData(Intent intent) {
   uname = intent.getStringExtra("nom");
    prenom = intent.getStringExtra("prenom");
    password = intent.getStringExtra("password");
    tel = intent.getStringExtra("tel");
    mobile = intent.getStringExtra("mobile");
    email = intent.getStringExtra("email");
}

0 个答案:

没有答案