为什么必须多次按下按钮才能执行操作?

时间:2019-09-10 13:02:47

标签: java android server android-asynctask

我正在用Android Studio做一个应用。到目前为止,我已经创建了注册和登录阶段。

在注册中,最重要的检查是用户,邮件和电话号码必须唯一(与较小的检查(例如regex)相比),在登录阶段检查用户是否存在,然后检查密码是否正确。就像我在数据库中一样,最重要的是用户未被锁定(数字从0到3,以报告输入通行证时出现的错误)。

这是怎么回事?当我输入数据(用于登录和注册)并按下按钮以启动验证访问/注册的检查时,将执行所有检查,但整个功能崩溃。让我举一个例子:检查用户名,然后检查用户名并检查电话,在检查用户名,检查电话并检查邮件之后,将一起执行功能,并回复产品结果的用户(例如:错误的电话号码)。

仅当存在AsyncTask时才会发生。我说为每个连接我都设置了一个7秒钟的计时器来进行连接,但是每次“第一次”都不会持续半秒(ProgressDialog出现并消失,并且没有Toast编程出现,而在接下来的执行中ProgressDialog会持续时间确定并正确响应操作结果。

活动-单击按钮

public synchronized void provapost(View v) {

    boolean ci = controllointernet(), cs = false, cn = false, cm = false, ct = false, cp = false, cname = false, cc = false, cl = false, cd = false;

    if (ci) {
        cn = controllonickname();
    } else...

    if (cn) {
        ct = controllotelefono();
    } else...

   if (ct) {
        cm = controllomail();
    } else...

controllonickname方法(类似于controllotelefono和controllomail)

String u = user.getText().toString();
ControlloUsername task = new ControlloUsername(this);
task.execute(u);
task.setOnFinishListener(new ControlloUsername.onFinishListener() {
       @Override
       public void onFinish(String res) {
           // The task has finished and you can now use the result
           if (res.contains("xxx")) {...
              ...
              ...
       ...}
        });

ControlloUsername(始终类似于其他Asynctasks)

public class ControlloUsername extends AsyncTask<String, Void, String> {

// TODO Auto-generated method stub
Context context;
ProgressDialog a;
onFinishListener mListener; // Create a variable for your interface

// Define the interface & callbacks
public interface onFinishListener{
    // Replace 'variableType' with the appropriate type for your result
    void onFinish(String myResult);
}

public void setOnFinishListener(onFinishListener l){
    mListener = l;
}

public ControlloUsername(Context ct) {
    // TODO Auto-generated constructor stub
    context = ct;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    a=new ProgressDialog(context);
    a.setTitle("Send");
    a.setMessage("Sending..Please wait");
    a.show();
}

@Override
protected void onPostExecute(String result) { //sync
    if(mListener != null){
        mListener.onFinish(result);
    }
    a.dismiss();
}

@Override
protected void onProgressUpdate(Void... values) {
    a.show();
}

@Override
protected String doInBackground(String... params) { //sync
    // TODO Auto-generated method stub

    String username = params[0];
    String login_url = Connector.db + "script/usercontrol.php";
    String resultado = "";

    URL u = null;
    try {
        u = new URL(login_url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpURLConnection http = null;
    try {
        http = (HttpURLConnection) u.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        http.setRequestMethod("POST");
    } catch (ProtocolException e) {
        Writer writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        resultado = writer.toString();
    }
        http.setDoInput(true);
        http.setDoOutput(true);



    try {
        http.setConnectTimeout(5000);
        OutputStream out = http.getOutputStream();
        BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
        String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
        bf.write(post_data);
        bf.flush();
        bf.close();
        out.close();
        InputStream in = http.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in, "iso-8859-1"));
        String result = "";
        String line = "";
        while ((line = br.readLine()) != null) {
            result += line;
        }
        bf.close();
        in.close();
        http.disconnect();
        return result;
    } catch (ConnectException e) {
        ...
}
...
}

视频:https://youtu.be/BLIp7YmiphM

在视频示例中,我首先验证用户是否存在,然后检查密码,如果输入错误,我将减少可能的错误数量,直到将帐户阻止为“第四个错误”为止(这是第七个)。按了三下,我启动了userControl / passControl / refreshErrors-AsyncTasks(大约接下来的几次,我认为在将数据取入和放到db的方法之间存在同步问题。

我之前遇到过问题,因为AsyncTasks阻止了用户界面,所以我使用了这种替代方法。可能是OnPostExecute的未使用和未实现吗?

那会是什么?非常感谢:)

0 个答案:

没有答案