在android中创建用户时检查用户名可用性

时间:2014-12-18 18:19:44

标签: java android json

我正在创建我的第一个android,它将从注册表单中获取数据并将其发送到php后端。

php后端将获取数据并保存在数据库中,并提供jason编码消息,告知它是否成功。

现在我想消除dupilicate用户名的可能性,所以当android应用程序将数据发送到php后端时,我将首先检查,如果它是重复的,我将抛出这样的错误消息

$response["error"] = true;
$response["message"] = "Username Already taken";
echoRespnse(400,$response);

在成功后,后端会发送类似这样的内容

$response["error"] = false;
$response["message"] = "Successfuly Registered";
echoRespnse(201,$response);

如何启用Android应用程序以阅读此信息并了解用户是否已创建或发生错误。

我当前的Android signup.java代码如下所示

public void post() throws UnsupportedEncodingException
    {
        // Get user defined values
        uname = username.getText().toString();
        email   = mail.getText().toString();
        password   = pass.getText().toString();
        confirmpass   = cpass.getText().toString();
        phone = phn.getText().toString();

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.rgbpallete.in/led/api/signup");
        if (password.equals(confirmpass)) {
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
                nameValuePairs.add(new BasicNameValuePair("uname", uname));
                nameValuePairs.add(new BasicNameValuePair("pass", password));
                nameValuePairs.add(new BasicNameValuePair("email", email));
                nameValuePairs.add(new BasicNameValuePair("phone", phone));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);
                //Code to check if user was successfully created
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            Toast.makeText(getBaseContext(), "Password mismatch", Toast.LENGTH_SHORT).show();
            //Reset password fields
            pass.setText("");
            cpass.setText("");
        }

    }

3 个答案:

答案 0 :(得分:1)

你可以犯&#34;错误&#34;一个int而不是一个布尔值,让你的php后端返回特定的错误代码。这将允许您的Android应用程序了解特定的错误。如果没有这种修改,检查特定字符串的消息值是另一种选择。

例如,如果没有错误,您可以返回0;如果已经使用了用户名,则返回1;如果是等等,则返回2.等等。

答案 1 :(得分:1)

我认为您需要帮助来获取并阅读您的服务提供的JSON数据,对吧? 在您的SignUp Activity中创建一个AsyncTask,因为您无法在主线程上执行此操作。

private class DownloadOperation extends AsyncTask<Void, Void, String> {
    String uname = "";
    String email   = "";
    String password   = "";
    String confirmpass   = "";
    String phone = "";

     @Override
protected void onPreExecute() {
    super.onPreExecute();
    // Get user defined values
    uname = username.getText().toString();
    email   = mail.getText().toString();
    password   = pass.getText().toString();
    confirmpass   = cpass.getText().toString();
    phone = phn.getText().toString();
}

@Override
protected String doInBackground(Void... params) {
        String response = "";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.rgbpallete.in/led/api/signup");
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("uname", uname));
            nameValuePairs.add(new BasicNameValuePair("pass", password));
            nameValuePairs.add(new BasicNameValuePair("email", email));
            nameValuePairs.add(new BasicNameValuePair("phone", phone));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);
            httpResponse = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
        return response;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    Log.d("tag", "Result:\n" + result);
}}

然后致电

// Calling async task to get json
new DownloadOperation().execute();

您将在控制台上看到json字符串:)

使用响应字符串获取JSONObject:

JSONObject jsonObj = new JSONObject(STRING);

希望有所帮助。

答案 2 :(得分:-1)

在注册用户并插入数据库之前,检查数据库中用户名的查询。如果找到用户名,则将json值编码为错误

$ query = mysql_query(&#34;从yourtable中选择id,其中username =&#39; $ username&#39;&#34;); 如果(mysql_numnum_rows($查询)&0)

&#13;
&#13;
// example for response
//responses from server for success
response["success"]=1;
response["message"]='No error code'

//responses from server for duplicate username

response["success"]==0
response["message"]='Username exists';



// java code
// after getting string from server parse in into json object
JSONObject jsonObj = new JSONObject(STRING);
int success = jsonObj.getInt("success");
				message = jsonObj.getString("message");
				if (success == 1) {
					// register successfully
				} else {
                  // username already exist
                  
				}
&#13;
&#13;
&#13;