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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:28:57+00:00 2026-06-17T12:28:57+00:00

I am running a function inside Async task to get feed from a url

  • 0

I am running a function inside Async task to get feed from a url on my server, it works but when there is no Internet connection it stops my application by giving a error message.

I am not sure how to handle this, anyways here goes my code

FUNCTION TO FETCH FEED

   public void getContent(){
    // Initializing instance variables
    headlines = new ArrayList<String>();
    links = new ArrayList<String>();
    server_images = new ArrayList<String>();

    try {
        URL url = new URL("http://www.uglobal.org/androidServer/courses_list.xml");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

            // We will get the XML from an input stream
        xpp.setInput(getInputStream(url), "UTF_8");

            /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag.
             * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag.
             * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...."
             * so we should skip the "<title>" tag which is a child of "<channel>" tag,
             * and take in consideration only "<title>" tag which is a child of "<item>"
             *
             * In order to achieve this, we will make use of a boolean variable.
             */
        boolean insideItem = false;

            // Returns the type of current event: START_TAG, END_TAG, etc..
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)
                        headlines.add(xpp.nextText()); //extract the headline
                } else if (xpp.getName().equalsIgnoreCase("link")) {
                    if (insideItem)
                        links.add(xpp.nextText()); //extract the link of article
                } else if (xpp.getName().equalsIgnoreCase("image")) {
                    if (insideItem)
                        server_images.add(xpp.nextText()); //extract the link of article
                }
            }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                insideItem=false;
            }

            eventType = xpp.next(); //move to next element
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public InputStream getInputStream(URL url) {
   try {
       return url.openConnection().getInputStream();
   } catch (IOException e) {
       return null;
     }
}

Than i run this function inside the async task

ASYNC TASK

    private class PostTask extends AsyncTask<String, Integer, String>{
    @Override

    protected void onPreExecute() {
        // TODO Auto-generated method stub
        progressDialog = ProgressDialog.show(ListViewImagesActivity.this, "Loading","Getting Feed ", true);
    }
    protected String doInBackground(String... arg0) {
        getContent();
        return null;
    }
    }

Now if i have Internet connection it works fine but the moment i loose connection it kills my application with an error “Application Not Responding”

WHY I DON”T WANT TO RELY ON Connectivity Manager

Connectivity Manager just looks for the Internet connection , what if i am running behind a proxy and i need to be logged to my proxy server in order to access Internet ?

Is there any way to at least prevent my application from getting killed.

  • 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-17T12:28:58+00:00Added an answer on June 17, 2026 at 12:28 pm

    Quick answer

    Sure, handle the exceptions on try/catch blocks: Even if the IDE wont tell you a function throws an exception they can be raised, in fact that seems to be your case right now.

    Try running your application while completely offline and wrap each and every line that raises an exception in a try/catch block (Off course, a single try/catch for the whole “offending” code is enough as long as you catch all exceptions).


    Tips and tricks

    As for not wanting to rely on ConnectivityManager? That’s a bad idea. ConnectivityManager may not handle edge cases as college proxies and whatnot, but its still a protection layer. You dont want to waste precious computation time starting connections that you can know in advance will invariably fail, specially on a mobile phone.

    Pro tip: You might want to check the presence of proxies in your own code before feeding the downloaded strings to the XML parsers, just check the first line. If that web service outputs proper XML it will most likely contain something around the likes of

    <?xml version="1.0" encoding="UTF-8" ?>
    

    so if the first line is not starting with something similar you can be sure its not XML.

    Conversely, you can know if your requests got hijacked into a proxy login page by checking if the first line of the request for something like

    <!DOCTYPE....
    

    or

    <html....
    

    that’s pretty straightforward and its a classic behaviour for http proxies.

    Check this link (With special care on the readTwitterFeed() method). Even if its not for XML it will help you see how a standard RESTful WebService call is done on Android (One way at least). Again, just plug the output from that RESTful call into the filters i described above and then into your favorite XML parser (Assuming the output did pass those sanitation checks).

    Bottomline is:
    Do not make your own work that much harder. Don’t re-invent the wheel and use what’s available to you in clever ways.

    Cheers.

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

Sidebar

Related Questions

Is there any way for using an annotation for running a function before currently
I've read numerous similar questions, but the answers simply didn't work. Running this function
Inside my Application class, I have a long-running function (e.g. downloading new data). Once
I am running an ajax function that calls an external page but am having
I use Paul Irish Smartresize but when I resize the window the function inside
I have a PL/SQL function (running on Oracle 10g) in which I update some
I'm having some trouble trying running a function only once when onresize event is
I have a program in Octave that has a loop - running a function
I'm running a MSSQL function and returning to PHP and I'm using the function
i have small function that running on condintion, if text box value is not

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.