Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Retrieve a web page content in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 25-02-2010
Member
 
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());
}
Reply With Quote
  #3  
Old 25-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 25-02-2010
Member
 
Join Date: Nov 2009
Posts: 447
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.
Reply With Quote
  #5  
Old 25-02-2010
Member
 
Join Date: Nov 2009
Posts: 520
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 ()).
Reply With Quote
  #6  
Old 25-02-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Retrieve a web page content in java"
Thread Thread Starter Forum Replies Last Post
Unable to retrieve page in kindle based on page numbers rUChIRr Portable Devices 6 08-07-2011 11:02 PM
Retrieve facebook fan page in PHP Macario Software Development 5 28-11-2010 02:46 PM
Retrieve multiple values from java class Ash maker Software Development 5 13-02-2010 12:05 AM
How to retrieve content advisor password? Abhiraj Windows Software 3 08-08-2009 01:06 AM
How to retrieve XML data & display on the page in the table? VijayW Software Development 2 16-02-2009 09:56 PM


All times are GMT +5.5. The time now is 12:14 PM.