Search code examples
javaandroidandroid-asynctaskandroid-handlerandroid-looper

how to call method with an asynctask after button is clicked


How can you call a method within an AsyncTask? In my asynctask,which is an inner class in my java file 'xyz', when the user clicks a button, it should call a method within 'xyz' which also happens to be an alertDialog, i know it calls it, but when it reaches the method, the app crashes and gives a runtime exception, which says 'Can't create handler inside thread that has not called Looper.prepare()'. I looked up examples here but it threw the same runtime exception. How can i make it work? isn't calling an outer method from within the asynctask a possibility? this is the snippet to call the method:

 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);
        }

        }
    }
}

and this is the method called:

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();
}

Solution

  • It looks like you need doInBackground to return true or false when it's finsished. You need doInBackground to return the boolean. Try this:

    @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
    }
    

    Then your onPostExecute should look like this:

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