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 8533537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:58:59+00:00 2026-06-11T09:58:59+00:00

I have this really crazy problem which I cant seem to tackle. I tried

  • 0

I have this really crazy problem which I cant seem to tackle. I tried many variations to my approach but it doesnt seem to work like I intended.
What I want is when the user clicks a button on my MainActivity, it should spawn a new thread made available by AsyncTask. The MainActivity should display a ProgressDialog in the meanwhile. The newly created thread is incharge of using a HTTP connection to fetch xml through a url, parse it and store the data in a local database.

What I want is the AsyncTask thread to throw a timed out exception, or any exception in case there is no data connection or if the xml is not available at the url. It must dismiss the ProgressDialog at that time.

This is what I did, sometimes it works but at times it just shows the ProgressDialog for a long time

class RetreiveFeedTask extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        pd = ProgressDialog.show(MainActivity.this, "Working..",
                "Fetching Values", false,true);

    }

    protected String doInBackground(String... params) {
        String xml = null;
        try {

            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
                    false);

            HttpGet method = new HttpGet(
                    new URI(
                            "http://www.blahblahblah.xml"));
            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 5000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            int timeoutSocket = 5000;
            HttpConnectionParams
                    .setSoTimeout(httpParameters, timeoutSocket);

            DefaultHttpClient client = new DefaultHttpClient();


            HttpResponse res = client.execute(method);

            InputStream is = res.getEntity().getContent();
            parser.setInput(is, null);


            int eventType = parser.getEventType();

            parser.nextTag();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (parser.getAttributeCount() == 2) {

                        myDbHelper.update(parser.getAttributeValue(0),
                                parser.getAttributeValue(1));

                    }
                }
                eventType = parser.next();
            }
            System.out.println("End document");

            xml = "Done, Database updated :)";
        } 


        catch (Exception e) {
            xml = "Error: Unable to fetch data";
             e.printStackTrace();

        }
        return xml;
    }
@Override
    protected void onPostExecute(String xml) {
pd.dismiss();
        Toast.makeText(MainActivity.this, xml, Toast.LENGTH_SHORT).show();
    }

}
  • 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-06-11T09:59:00+00:00Added an answer on June 11, 2026 at 9:59 am

    best way is

    you can check data connectivity before starting async task

    like

    boolean isInternetConnectd;

    if(isInternetConnectd)
    {
    // do network related task….
    }
    else
    {
    // show dialog “internet connection not available”
    }

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

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
I have this concrete problem, but if You find my initial design idea crazy
Ok I have this problem that I've never had before, it's really bugging me.
this seems to be weird but it really happened. I have this code working
I have read that you can do it, but would this really improve performance
I have a little problem..but im goin' to crazy... In the html i have
I've got this simple problem that is really making me crazy. I've got a
Ok, this might be really crazy, and/or stupid but.. I am writing an ircbot
This is a simple stuff but driving me really crazy now. Spent hours on
this problem has driven me crazy and I really need you guys help. I

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.