Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

url reader applet

 
vikram kamath c
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my appet opens many url connections and reads data from it . it is very slow is there a way to make it fast
URL url = null;
url2[q] = "http://www.url.com";
fileToRead=url[q];
try{
url = new URL(getCodeBase(), fileToRead);
}
catch(MalformedURLException e){}

try{
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
strBuff1 = new StringBuffer();
String inputLine,m="";
int k=0,c=0;
while ((inputLine = bf.readLine()) != null)
{}
.....
........
........

can i open many connections simutaneuosly i am using a for loop to open connections

please help
thanks
 
Oleg Tikhonov
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, try this:


and this one:


Hope it helps.
Oleg.
 
vikram kamath c
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use the code on the top with a for loop .i think what you have given is almost the same as i use can i make it faster by opening connections simultaneously to many sites not one after the other so time is saved is it possible to do that
,,, thanks in advance
 
vikram kamath c
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i use
for(i=0;i<20;i++)
# try {
# URL yahoo[i] = new URL(getMUrl());
# URLConnection yc = yahoo[i].openConnection();
# BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
# String inputLine;
#
# while ((inputLine = in.readLine()) != null)
# System.out.println(inputLine);
# in.close();
# } catch (Exception e) {
# e.printStackTrace();

something like this but with many methods to call separate domains
please help
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikram kamath c wrote:i think what you have given is almost the same as i use

It's not the same by a long shot.

can i make it faster by opening connections simultaneously to many sites not one after the other so time is saved

Yes that's possible, and it's what Oleg's code (but not your code) does.

BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

This is correct only if the encoding of the pages read from all the external sites is the same as the platform default encoding of the machine running the applet. For example, www.coderanch.com uses UTF-8, while java.sun.com uses ISO-8859-1, so the code isn't even correct for those two sites.
 
vikram kamath c
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you suggest how to use Oleg's code for my program .i don't have much knowledge about threads Thank you Oleg,Thank you Ulf dittmer i have 5 methods which use run like the one i showed in the third thread .thanks .....
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oleg's code looks like a complete application to me; did you get it to run? If so, you need to refactor the URLConnectionReader class from an application to an applet (which chiefly means replacing the "main" method by suitable "init" and "start" methods).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic