Results 1 to 6 of 6

Thread: Retrieve a web page content in java

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Retrieve a web page content in java

    Hello,
    I'm new here, I started in Java (well since the beginning of the year: d)
    and I try to create a class that makes it

    Fetches the number of results
    It gets the URL of the first (or nth but to implement)
    It connects to this URL
    It retrieves information selected
    Storage SQL

    Is it possible to specify the limits of my InputStream? Because when I try to compile the code, it gives me a error. I think I have a trouble to retrieve a web page content. If you have already done this then please help me.

  2. #2
    Join Date
    Nov 2009
    Posts
    359

    Re: Retrieve a web page content in java

    Hello,
    Try changing the way of reading the page, that is you can try the following code:
    Code:
    StringBuffer src = new StringBuffer();
    
    URLConnection url = u.openConnection();
    url.setUseCaches(false);
    url.connect();
    
    Scanner sc = new Scanner(url.getInputStream());
     
    / / While there are still lines and we have not finished collecting data
    while(sc.hasNextLine()){
    	src.append(sc.nextLine());
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Retrieve a web page content in java

    Hello,
    If you really want to read character by character, you can also do as before but with a buffer:
    Code:
    int b;
    InputStream input = u.openStream();
    StringBuffer s = new StringBuffer();
    while ((b = input.read()) != -1){
    	s.append((char) b);
    } / * while * /
    / / EOF reached
    input.close();
    This should work better than what you are using in your code.

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Retrieve a web page content in java

    Hello,
    When you are trying to do this
    Code:
    while ((b = is.read()) != -1)
    		s + =(char) b; / * while * /
    You create a string of size 1 then 2 then 3 etc.. So you create a lot of objects in memory and it is expensive. Use StringBuilder instead (or if StringBuffer java <5). So, if this is what you want then you can use it in your code, but if this is not your need, then you have to think about an alternative.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Retrieve a web page content in java

    Hello,
    Besides AC costs even more expensive since you also creates a StringBuilder for each iteration, as this code:
    Code:
    s + =(char) b;
    Is equivalent to the following code:
    Code:
     s = new StringBuilder(s).append(b).function toString() {
        [native code]
    }();
    So you have two new objects (new StringBuilder () and toString ()).

  6. #6
    Join Date
    Nov 2009
    Posts
    335

    Re: Retrieve a web page content in java

    Hello,
    Indeed, the Scanner class (if you speak well of it) can play a stream more easily. But you can spend quite a StringBuffer is equally. When you get the int, you must know that this is int there and instead add it to the StringBuffer, he will just put it in another variable not? If you need more information on the same then you can visit the sun's official site and there you can find the documentation on this.

Similar Threads

  1. Unable to retrieve page in kindle based on page numbers
    By rUChIRr in forum Portable Devices
    Replies: 6
    Last Post: 08-07-2011, 10:02 PM
  2. Retrieve facebook fan page in PHP
    By Macario in forum Software Development
    Replies: 5
    Last Post: 28-11-2010, 02:46 PM
  3. Retrieve multiple values from java class
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 12:05 AM
  4. How to retrieve content advisor password?
    By Abhiraj in forum Windows Software
    Replies: 3
    Last Post: 08-08-2009, 12:06 AM
  5. How to retrieve XML data & display on the page in the table?
    By VijayW in forum Software Development
    Replies: 2
    Last Post: 16-02-2009, 09:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,719,025,273.02197 seconds with 17 queries