drzrdc1766788 2015-06-23 21:33
浏览 15

Android App将数据上传到MySQL,仍然会返回错误?

I am creating an app that uploads user data to a MySQL database. The app uploads the data upon request and can be seen as a record inside of the database, but the app returns an error and doesn't let the user move on.

Here's the Android code I'm using:

private void registerUser(final String firstname, final String lastname, final String email,
                          final String password) {
    String tag_string_req = "req_register";

    pDialog.setMessage("Registering ...");
    showDialog();

    StringRequest strReq = new StringRequest(Method.POST, AppConfig.URL_REGISTER, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("ME", "Register Response: " + response);
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    String uid = jObj.getString("uid");


                    JSONObject user = jObj.getJSONObject("user");
                    String firstname = user.getString("firstname");
                    String lastname = user.getString("lastname");
                    String email = user.getString("email");
                    String created_at = user.getString("created_at");

                    db.addUser(firstname, lastname, email, uid, created_at);

                    Toast.makeText(getApplication(), "Welcome! Please Login", Toast.LENGTH_LONG).show();

                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    //**This is the error I'm receiving**//
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(), errorMsg + ", Error in RegisterActivity1", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplication(), "Couldn't connect to Database", Toast.LENGTH_LONG).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage() + ", Error in RegisterActivity2", Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("tag", "register");
            params.put("firstname", firstname);
            params.put("lastname", lastname);
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };

    // Adding request to request queue
    MyApplication.getInstance().addToRequestQueue(strReq, tag_string_req);
}

The PHP storeuser function:

public function storeUser($firstname, $lastname, $email, $password) {
    $userid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = mysql_query("INSERT INTO userinfo_basic(userid, firstname, lastname, email, password, salt, created_at) VALUES('$userid', '$firstname', '$lastname', '$email', '$encrypted_password', '$salt', NOW())");
    // check for successful store
    if ($result) {
        // get user details 
        $uid = mysql_insert_id(); // last inserted id
        $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
        // return user details (true)
        return mysql_fetch_array($result);
    } else {
        return false;
    }
}

And when the storeuser function gets called

if ($tag == 'register') {
    // Request type is Register new user
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // check if user is already existed
    if ($db->isUserExisted($email)) {
        // user is already existed - error response
        $response["error"] = TRUE;
        $response["error_msg"] = "User already existed";
        echo json_encode($response);
    } else {
        // store user
        $user = $db->storeUser($firstname, $lastname, $email, $password);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Error occured in Registartion";
            echo json_encode($response);
        }
    }
}

I'm not sure what's going wrong, so all help is appreciated.

I can show more code or clarify upon request.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度