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

标签 java android json

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

php 后端将获取数据并保存在数据库中,并给出一个 jason 编码的消息,告诉它是否成功。

现在我想消除重复用户名的可能性,所以当 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("");
        }

    }

最佳答案

我想您需要帮助来获取和读取您的服务提供的 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);

希望对您有所帮助。

关于java - 在android中创建用户时检查用户名可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553154/

相关文章:

java - 如何找到给定字符串的最长重复子串

来自 MainActivity 的 requestPermission 自动拒绝 Android 权限

java - Android 中无标签字符串的 JSON 解析

javascript - Iron-ajax 响应为 php json 响应返回 null

json - 如何在 Gherkin 的场景大纲中使用类似文档字符串的内容?

java - 使用帧缓冲区错误 Cubieboard A10 运行 JavaFX 应用程序

java - JDB 面临问题 - 未命中断点

java - 在 RCP TableViewer 中手动触发 CellEditor

android - 选项卡中的图标不显示!!!我应该怎么办?

android - string.xml 国际化问题