android应用程序返回空吐司

xfyts7mz  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(297)

当我尝试将用户注册到我的sql数据库时,得到一个空toast作为响应,而logcat中没有抛出任何有用的错误提示。
请问这可能是什么原因? 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();
}
}

web服务

/**

  • 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;
}
}

bvuwiixz

bvuwiixz1#

据我所知,代码中似乎没有错误。您应该检查curl得到的响应,看看返回了什么。

9gm1akwq

9gm1akwq2#

为了从web服务获得响应,您不需要json,因为您正在通过volley执行post请求。你的祝酒词是空的,因为你只是错误地解释了响应和像这样的对象 String errorMsg = jObj.getString("message"); 将为空。
要得到响应,您应该这样做:

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

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

    ....
    Toast

} else{

    ....
    Toast      
}

编辑:
我在您的web服务代码中发现了两件事。
1-为什么你要从同一个用户的服务器上获取信息,而你只是从你的应用程序中发送信息。那没道理。你应该附加你的数据作为额外的你的意图,因为你与电子邮件。
2-volley需要一个字符串类型的响应,而你同时给他不同的格式。
所以这是您的代码的修改版本:
web服务:

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";
    }
}

安卓

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请求中的错误,请不要在你的应用程序上显示它,只需在浏览器中检查它。
我希望这对你有用

相关问题