user3191769
user3191769

Reputation: 31

AsyncTask make the application crash

I'm trying to contact my database using .php It works fine the first time the ASYNC runs, though the second time i run it (without exiting the application) the application crashes (cant catch the error message from LogCat)

And also for some reason it won't enter the while-loop at all. I've written code like this many times before and it worked out just fine, though not this time.

Code:

class LOAD_USERS extends AsyncTask<String, Void, Void>
{
    //Internet Input
    URL url;
    InputStream iS;
    InputStreamReader iSR;
    BufferedReader r;

    //Variables
    List<String> Users = new ArrayList<String>();
    public String s = "";
    public String DOWNLOAD_SUCCESS = "fail";
    String charset = "iso-8859-1";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MenuActivity.this);
        pDialog.setMessage("Letar efter spelare");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(String... sUrl) {
        try{
            url = new URL(sUrl[0]);
            iS = url.openStream();
            iSR = new InputStreamReader(iS, charset);
            r = new BufferedReader(iSR);

            Users.clear();

            while((s = r.readLine()) != null)
            {
                Users.add(s);
                DOWNLOAD_SUCCESS = "success";
            }
        }catch(Exception e)
        {
            Log.e(e.toString(),e.toString());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);
        pDialog.dismiss();
        if(DOWNLOAD_SUCCESS.equalsIgnoreCase("success"))
        {
            MenuActivity.this.CheckValidation();
        }
        else if(DOWNLOAD_SUCCESS.equalsIgnoreCase("fail"))
        {
            Toast.makeText(getBaseContext(), "Check Your Internet Connection",   Toast.LENGTH_SHORT).show();
        }

        DownloadComplete = true;
    }
}

If you could catch my error, that would be great. Thank you!

Upvotes: 2

Views: 509

Answers (2)

molnarm
molnarm

Reputation: 10031

From the AsyncTask documentation (Threading rules section):

The task can be executed only once (an exception will be thrown if a second execution is attempted.)

Upvotes: 1

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

My guess is that Async tasks created to run once, meaning you need to create a new instance of your class.

Upvotes: 0

Related Questions