在从我的数据库中检索数据时实施 Asynctask

Implement Asynctask in retrieving data from my database

我怎样才能做到这一点?我正在创建注册表单。我不想等到完成从我的数据库中检索数据后再继续另一个领域。示例:我的第一个字段是学生 ID,现在在那里输入数据后,它将使用 HTTPUrlConnection 从数据库中检查它是否已被使用。但有时特别是如果我使用免费的虚拟主机,它需要时间才能完成,但有时我的应用程序没有响应。我想要的是在我填写其他字段时让它检查。

来自我的电子邮件验证的示例代码

 public boolean checkIfSameEmail(String data) {
    try {
        String accountURL =  DataClass.localAddress + "/android_php/account.php";
        URL url = new URL(accountURL);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        StringBuffer buffer = new StringBuffer();
        String line = "";


        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }

        String finalJson = buffer.toString();

        JSONObject parentObject = new JSONObject(finalJson);
        JSONArray parentArray = parentObject.getJSONArray("users");

        for (int x = 0; x < parentArray.length(); x++) {
            JSONObject finalObject = parentArray.getJSONObject(x);

            if (finalObject.getString("email").equalsIgnoreCase(data)) {
                System.out.println("Here!!");
                return true;

            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

然后

  edtEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            edtEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!edtEmail.hasFocus()) {
                        getData();
                        edtEmailET(sEmail);
                    }
                }
            });
        }
    });

我必须使用线程、asynctask 还是什么?我是这里的初学者,所以我真的没有想法。我只是在从数据库中插入数据时使用 Asynctask,我只是从教程中学到的,也许我会在这里使用 onPreExecute()?

更新:

edtEmailET 内部是这样的:

    public boolean edtEmailET(String Email) {

    if (Email.replace(" ", "").isEmpty()) {
        tilEmail.setError("You can't leave this empty.");
        return true;
    } else if (!checkIfValidEmail(Email) == true) {
        tilEmail.setError("Invalid e - mail address");
        return true;
    } else if (checkIfSameEmail(Email) == true) {
        tilEmail.setError("E-mail already used.");
        return true;
    } else {
        tilEmail.setError(null);
        return false;
    }
}

使用 AsyncTask 应该可以满足您的需要。 您可以使用如下结构:

class VerifyEmailTask extends AsyncTask<String, Void, Boolean> {
    // use doInBackground() to make network calls, the returned value is
    // sent to onPostExecute()
    @Override
    protected Boolean doInBackground(String... data) {
        return checkIfSameEmail(data[0]);
    }

    // use onPostExecute() to perform any desired operation on the UI
    // with the result from your network call
    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            // do something
            Log.d("VerifyEmailTask", "Here!!");
        } else {
            // do something else
        }
    }
}

并调用它:

new VerifyEmailTask().execute("email@domain.com");

您应该阅读 AsyncTask (https://developer.android.com/reference/android/os/AsyncTask.html) 的文档。