单击按钮后如何使用异步任务调用方法

how to call method with an asynctask after button is clicked

如何在 AsyncTask 中调用方法?在我的异步任务中,它是我的 java 文件 'xyz' 中的一个内部 class,当用户单击一个按钮时,它应该调用 'xyz' 中的一个方法,这也恰好是一个 alertDialog,我知道它会调用它,但是当它到达该方法时,应用程序崩溃并给出运行时异常,该异常显示“无法在未调用 Looper.prepare() 的线程内创建处理程序”。我在这里查找示例,但它引发了相同的运行时异常。我怎样才能让它工作?从 asynctask 中调用外部方法不是一种可能性吗? 这是调用方法的片段:

 private class DownloadFilesTask extends AsyncTask<Void,Void,Void> {

    private LoginActivity loginActivity;

    public DownloadFilesTask(LoginActivity loginActivity){
        this.loginActivity=loginActivity;
    }


    @Override
    protected Void doInBackground(Void... voids) {
        long start=System.currentTimeMillis();
        in=null;
        try {
            website=new URI(URL);
            request.setURI(website);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        HttpPost httpPost=new HttpPost(URL);
        List<NameValuePair>nameValuePairs=new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("name",name));
        nameValuePairs.add(new BasicNameValuePair("pwd",pwd));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {

            response=httpClient.execute(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String line=in.readLine();
            long end=System.currentTimeMillis();
            long times=end-start;
            String time=String.valueOf(times);
            System.out.println(name);
            System.out.println(NAME_PATTERN);
            System.out.println(pwd);
            System.out.println(PWD_PATTERN);
             if (name.equals(NAME_PATTERN) && (pwd.equals(PWD_PATTERN))) {
                bloggedIn=true;
                System.out.println("Youre right");

            }else
            {
                bloggedIn=false;
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void onPostExecute(String line, String time) {
        if(bloggedIn=true){
            navigateToMainActivity(time);
        }else{ if (bloggedIn=false){
            newp(line,time);
        }

        }
    }
}

这是调用的方法:

public void navigateToMainActivity(String timetoo) {
    al=new AlertDialog.Builder(LoginActivity.this);
    al.setTitle("Welcome");
    al.setMessage("Hey there");
    al.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            startActivity(new Intent(LoginActivity.this, Main.class));
        }
    });
    al.show();
}

完成后,您似乎需要 doInBackground 到 return truefalse。您需要 doInBackground 到 return 布尔值。试试这个:

@Override
protected Boolean doInBackground(Void... arg0)
{
    // your stuff
    return bloggedIn; // instead of null or return the boolean where you are setting it true or false
}

那么你的 onPostExecute 应该是这样的:

 @Override
 protected void onPostExecute(Boolean result)
 {
    super.onPostExecute(result);
    if(result){
        navigateToMainActivity(time);
    }else{
        newp(line,time);
    }
 }