Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:53:46+00:00 2026-05-27T15:53:46+00:00

I’m using using AsyncTask to download data over internet and I have a little

  • 0

I’m using using AsyncTask to download data over internet and I have a little problem. I need to be able to start one AsyncTask a few times, that’s why I’m creating a new instance everytime,but the thing that I notice is that it’s working without any problem the first three or four times,but after that my AsyncTask is stuck on onPreExecute() and doing nothing after that. Am I doing something wrong ? (Actually I am using two AsyncTasks one after another just for testing purposes). Here is the sample code which I’m using :

this is how I start the AsyncTasks :

    if (index == 1) {
        //Login - first way
        new FirstSync().execute(Synchronization.this);
    } else if (index == 2) {
        //SyncWithHash - second way
        SyncWithHash syncHash = new SyncWithHash();
        syncHash.execute(Synchronization.this);
    } else if (index == 3) {
        //Deactivate Collection - third way
        deactivateColl = new DeactivateCollection();
        deactivateColl.execute(Synchronization.this);
    }

I did try with three different ways to start the asyncTask,but no change. Here is my AsyncTask :

    // Sync With Hash
public class SyncWithHash extends AsyncTask <Context, Integer, Void> {
    @Override
    protected Void doInBackground(Context... arrContext) {
        try {

            String charset = "UTF-8";
            hash = getAuthHash();

            SharedPreferences lastUser = PreferenceManager.getDefaultSharedPreferences(Synchronization.this);
            int userId = lastUser.getInt("lastUser", 1);

            systemDbHelper = new SystemDatabaseHelper(Synchronization.this, null, 1);
            systemDbHelper.initialize(Synchronization.this);
            String sql = "SELECT dbTimestamp FROM users WHERE objectId=" + userId;
            Cursor cursor = systemDbHelper.executeSQLQuery(sql);
            if (cursor.getCount() < 0) {
                cursor.close();
            } else if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                timeStamp = cursor.getString(cursor.getColumnIndex("dbTimestamp"));
                Log.d("", "timeStamp : " + timeStamp);
            }

                String query = String.format("debug_data=%s&"
                        + "client_auth_hash=%s&" + "timestamp=%s&"
                        + "client_api_ver=%s&"
                        + "set_locale=%s&" + "device_os_type=%s&"
                        + "device_sync_type=%s&"
                        + "device_identification_string=%s&"
                        + "device_identificator=%s&" + "device_resolution=%s",
                        URLEncoder.encode("1", charset),
                        URLEncoder.encode(hash, charset),
                        URLEncoder.encode(timeStamp, charset),
                        URLEncoder.encode(clientApiVersion, charset),
                        URLEncoder.encode(locale, charset),
                        URLEncoder.encode(version, charset),
                        URLEncoder.encode("14", charset),
                        URLEncoder.encode(version, charset),
                        URLEncoder.encode(deviceId, charset),
                        URLEncoder.encode(resolution, charset));

            SharedPreferences useSSLConnection = PreferenceManager
                    .getDefaultSharedPreferences(Synchronization.this);
            boolean useSSl = useSSLConnection.getBoolean("UseSSl", true);
            if (useSSl) {
                UseHttpsConnection(url, charset, query);
            } else {
                UseHttpConnection(url, charset, query);
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        //cancelDialog.setProgress(progress[0]);
    }
    @Override
    protected void onCancelled() {
        Log.d("","ON CANCELLED");
    } 
    @Override
    protected void onPreExecute() 
    {
        Log.d("","ON PRE EXECUTE");
       // myProgress = 0;
    }
    @Override
    protected void onPostExecute(Void v) {
        Log.d("","ON POST EXECUTE");
    }
  }

So any ideas why it’s happening and which is the best way to be able to use an AsyncTask a few times without any exceptions and bugs like the one that I get.

And another question : Is there anything in AsyncTask which can cause my connection to be Reset by peer , because I’m getting this error too (not every time).

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:53:47+00:00Added an answer on May 27, 2026 at 3:53 pm

    I think your doInBackground() is hanging. Make log statement when its entered and when its exited and check.

    In the old days AsyncTask had a pool of threads, so if a doInBackground() hung, then it didnt affect the other AsyncTasks. That changed AFAIK with Android 2.2 or 2.3 to that a single thread took care of all AyncTasks, one at a time. Therefore, if your doInBackground() is hanging it might affect the next AsyncTasks being started and the will hang right after onPreExecute().

    Edit: It was changed from a single thread, to multiple, and then back to a single thread:
    http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params…%29 “When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. After HONEYCOMB, it is planned to change this back to a single thread to avoid common application errors caused by parallel execution.”

    If you really want an indefinite lot of stuff to ‘hang’ in parralel, then don’t use AsyncTask. Use good old threads, which, when they need to update the GUI, fire off a Runnable to be runned on the GUI thread:

    Button knap1, knap2, knap3;
    ...
    
      Runnable r=new Runnable() {
        public void run() {
          // Do some stuff than hangs
          try { Thread.sleep(10000); } catch (InterruptedException ex) {}
          System.out.println("færdig!");
    
          // Update GUI thread
          Runnable r2=new Runnable() {
            public void run() {
              knap3.setText("færdig!");
            }
          };
          runOnUiThread(r2);
        }
      };
      new Thread(r).start();
    

    (example from http://code.google.com/p/android-eksempler/source/browse/trunk/AndroidElementer/src/eks/asynkron/Asynkron1Thread.java?spec=svn109&r=109)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.