应用程序返回空Toast

时间:2018-05-07 11:40:28

标签: android mysql database

当我尝试将用户注册到我的sql数据库并且没有在logcat中抛出有用的错误提示时,我得到一个空的Toast作为响应。

请问这可能是什么原因?

registerProcess

 private void registerProcess(final String name, final String email, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_register";

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

    StringRequest strReq = new StringRequest(Request.Method.POST,
            Functions.REGISTER_URL, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response);
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    Functions logout = new Functions();
                    logout.logoutUser(getApplicationContext());

                    Bundle b = new Bundle();
                    b.putString("email", email);
                    Intent i = new Intent(RegisterActivity.this, EmailVerify.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtras(b);
                    startActivity(i);
                    pDialog.dismiss();
                    finish();

                } else {
                    // Error occurred in registration. Get the error
                    // message
                    String errorMsg = jObj.getString("message");
                    Toast.makeText(getApplicationContext(),errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

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

        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };

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

private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}
}

网络服务

/**
 * Adding new user to mysql database
 * returns user details
 */

public function storeUser($fname, $lname, $email, $uname, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = mysql_query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$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
        return mysql_fetch_array($result);
    } else {
        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

据我所见,代码似乎没有错误。 您应该检查 curl 得到的答案,然后查看返回的内容。

答案 1 :(得分:0)

从您的Web服务获取响应,因为您正在执行Volley的POST请求,因此您不需要JSON。你的吐司是空的,因为你只是在解释错误的反应,而像String errorMsg = jObj.getString("message");这样的对象将是空的。

要获得响应,您应该这样做:

if (response.equalsIgnoreCase("yourResponseFromTheWebService")) {

    ...
    Toast
} else if (response.equalsIgnoreCase("OtherPossibleResponse")){

    ....
    Toast

} else{

    ....
    Toast      
}

编辑:

我设法在您的网络服务代码中发现了两件事。

1-当您刚从应用程序发送信息时,为什么要尝试从同一用户的服务器获取信息?这没有意义。您应该像使用电子邮件一样附加您的意图数据。

2- Volley期望一个String类型的响应,你同时给他不同的格式。

所以这是您的代码的修改版本:

网络服务:

public function storeUser($fname, $lname, $email, $uname, $password) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = mysql_query("INSERT INTO users(unique_id, firstname, lastname, email, username, encrypted_password, salt, created_at) VALUES('$uuid', '$fname', '$lname', '$email', '$uname', '$encrypted_password', '$salt', NOW())");
    // check for successful store
    if ($result) {
        echo "successful";
    } else {
        echo "notsuccessful";
    }
}

<强>的Android

 private void registerProcess(final String name, final String email, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_register";

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

    StringRequest strReq = new StringRequest(Request.Method.POST,
            Functions.REGISTER_URL, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response);
            hideDialog();


                if (response.equalsIgnoreCase("successful")) {
                    Functions logout = new Functions();
                    logout.logoutUser(getApplicationContext());

                    Bundle b = new Bundle();
                    //add other informations you need like name ect..
                    b.putString("email", email);
                    Intent i = new Intent(RegisterActivity.this, EmailVerify.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtras(b);
                    startActivity(i);
                    pDialog.dismiss();
                    finish();

                } else if(response.equalsIgnoreCase("notsuccessful")) {

                    Toast.makeText(getApplicationContext(),"new user wasn't registered successfully", 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(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };

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

private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}
}

注意:

如果你想知道你的Mysql请求中的错误没有在你的应用程序上显示,请在浏览器中查看。

我希望这对你有用