Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Read XML from Internet Hang

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Location
    Malaysia
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Read XML from Internet Hang

    Hi everyone, I'm a bit new in Java.

    I have the following code which will read a php responded XML file.
    It's running under a timer in 10 seconds (for checking a message from server)
    Everything is OK when the Internet line smooth, When the internet line disconnected, some this it's return error but still OK because it's going back to normal when the Internet line resume.

    But sometime when the Internet line not so smooth, The request will just hanging over there, including the timer also not moving untill you have to use "alt-Ctrl Delete" to end the entire application.

    Anybody know why? and how can i solve this problem.

    ##########################################
    public boolean send(String file,String action)
    { address += file + "?act=" + action + param;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLReader read = null;
    try{
    if(debug) System.out.println(address);
    SAXParser saxParser = factory.newSAXParser();
    read = saxParser.getXMLReader();
    saxParser.parse(address,DH);

    }catch(javax.xml.parsers.ParserConfigurationExcept ion e){
    e.printStackTrace();
    DH.msg = e.getMessage();
    // saxParser.
    }catch(java.io.IOException e){
    e.printStackTrace();
    DH.msg = e.getMessage();
    }catch(org.xml.sax.SAXException e){
    e.printStackTrace();
    DH.msg = e.getMessage();
    }
    return DH.status;
    }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Read XML from Internet Hang

    You need to be able to set some sort of timeout on your request to get the file. Whats the location of the file you are trying to pass into your XMLReader? Is the file somewhere on the internet?

    I'd refrain from doing it that way and instead using an HTTP request to get the file data and then use a SAXBuilder to build a jdom document from it.

    // Json

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Location
    Malaysia
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read XML from Internet Hang

    Yes, it's from internet, a php program.
    I'm wondering how can a set the time out?

    Thank

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Read XML from Internet Hang

    So the address you are passing in to your xmlReader.parse is an actual http link then I take it?

    I think you should use a normal urlConnection to get the data you need and then put it in a SAXBuilder.

    Here is a short example.

        public static void main(String[] args) throws Throwable {
            final URL url = new URL("http://svn.cometd.com/trunk/cometd-java/server/pom.xml");
            final URLConnection urlConnection = url.openConnection();
            urlConnection.setConnectTimeout(5000); // 5 seconds timeout
     
            final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
     
            if (bufferedReader != null) {
                final StringBuilder stringBuilder = new StringBuilder();
                String line;
     
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
     
                bufferedReader.close();
     
                final SAXBuilder builder = new SAXBuilder();
                final Document document = builder.build(new StringReader(stringBuilder.toString()));
     
                // You now have a JDOM document, use it as you please :)
                System.out.println(new XMLOutputter(Format.getPrettyFormat()).outputString(document));
            }
        }

    // Json

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Location
    Malaysia
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read XML from Internet Hang

    Thank you very much,
    I will try.

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Location
    Malaysia
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read XML from Internet Hang

    Hi Json,
    I already try the normal urlConnection as given above and i set the timeout to 2000 milisecond.
    I still face the same problem, my internet connection not stable, whenever the internet line slow of disconnected, the connection keep waiting and hang!

    by the way my application is actually pulling a message from an internet web (apache) server for any event happen. This pulling is running in a timer, initially i set the time to 10 second, I did increase the timer to 15 seconds but the connection hang still happen! any idea?

    it's because of the timer that I'm using?

    Thank

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Read XML from Internet Hang

    So if your connection disappears does the URL connection not throw an ioexception at you? Everything just hangs? That sounds strange.

    // Json

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Location
    Malaysia
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read XML from Internet Hang

    Hi Json,
    Yes, tha's happen, no exception, everything just stop there.
    u have any idea?

  9. #9
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Read XML from Internet Hang

    Not a clue to be honest

    // Json

Similar Threads

  1. Read and unread forums
    By Json in forum The Cafe
    Replies: 23
    Last Post: June 17th, 2010, 07:30 AM
  2. How to read URLs from a web page
    By abitha in forum Java Theory & Questions
    Replies: 1
    Last Post: September 17th, 2009, 12:36 PM
  3. How to Read a Portion of a File in Java?
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 7th, 2009, 04:16 PM
  4. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM
  5. Internet Filter to display some website
    By sundarjothi in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: May 15th, 2008, 05:03 AM