Improving Connections performance
Hello!
I'am working on a J2ME prototype of a Stock Ticker that reads from some servlet that presents some string with Stocks data.
This piece of code is called several times:
StringBuffer sb=new StringBuffer();
InputStream is=null;
int b;
try{
if (con==null)
con=(HttpConnection) Connector.open(url);
if(con!=null)
{
is=con.openInputStream();
while((b=is.read())!=-1)
{
sb.append((char) b);
}
texto=sb.toString();
}
else
texto="Cota es indisponveis...";
}
finally{
if (is!=null)
{
is.close();
is=null;
}
if (con!=null)
{
con.close();
con=null;
}
}
Although this code works fine, I want to make the connections and data read faster. So my question is:
-Is it possible to let the connection open and create several InputStream rellying in the same connection? I try with the code below, but an IOException occurs: input stream already open.
StringBuffer sb=new StringBuffer();
InputStream is=null;
int b;
try{
if (con==null)
con=(HttpConnection) Connector.open(url);
if(con!=null)
{
is=con.openInputStream();
while((b=is.read())!=-1)
{
sb.append((char) b);
}
texto=sb.toString();
}
else
texto="Cota es indispon veis...";
}
finally{
if (is!=null)
{
is.close();
is=null;
}
}
Can anyone help me on this, because I'am new in HttpConnections and all of that...
Thanks a lot
Marco
Hi maybe you'd like to try this. It's just in theory.
1) keep the HttpConnection open
2) open the inputstream
3) receive data
4) close the inputStream and then open it up again from the HttpConnection.
eg. Here's a example in PseudoCode
HttpConn.open.........
Inputstream.open......
InputStream.close.....
InputStream.open.....
InputStream.close....
If this doesn't work, then I believe you would have to close the HttpConnection as well and re-establish it.
eg HttpConn.open...
InputStream.open...
InputStream.close...
HttpConn.close....
HttpConn.open.....
I've try like you sugest and it didn't worked out...
StringBuffer sb=new StringBuffer();
InputStream is=null;
int b;
try{
if (con==null)
con=(HttpConnection) Connector.open(url);
if(con!=null)
{
is=con.openInputStream();
while((b=is.read())!=-1)
{
sb.append((char) b);
}
texto=sb.toString();
}
else
texto="Unvailable...";
}
finally{
if (is!=null)
{
is.close();
is=con.openInputStream();
is.close();
//is=null;
}
/*if (con!=null)
{
con.close();
con=null;
}*/
}
I've also try with J2SE to get a URLConnection and get several InputStream from it, but a Stream Closed Exception is raised...
import java.net.*;
import java.io.*;
//import java.util.Timer;
//import java.util.TimerTask;
import java.util.*;
public class Online
{
URL u=null;
HttpURLConnection h;
//private Timer timer=new Timer();
public Online()
{
try{
String s;
u= new URL("http://193.102.124.56:8080/servlet/Sss");
h=(HttpURLConnection) u.openConnection();
run();
for(int i=0;i<10000;i++)
{
if(i==1000 || i==2000 || i==3000 || i==4000 || i==5000 ||i==6000 || i==7000 || i==8000 || i==9000 || i==10000)
run();
}
//XX xx=this.new XX();
//timer.schedule(xx,0,15000);
}catch(Exception e){
//System.out.println(e.getMessage());
}
}
public void run()
{
String s;
BufferedReader i=null;
try{
i =new BufferedReader(new InputStreamReader(h.getInputStream()));
while( (s=i.readLine())!=null)
System.out.println(s);
if (i!=null)
{
i.close();
i=null;
}
}catch(Exception e){System.out.println(e.getMessage());}
}
public static void main(String args[]) throws IOException
{
StocksOnline si=new StocksOnline();
}
/*
class XX implements TimerTask
{
public void run()
{
String s;
BufferedReader i=null;
try{
i =new BufferedReader(new InputStreamReader(u.openStream()));
while( (s=i.readLine())!=null)
System.out.println(s);
}catch(Exception e){}
finally{
if (i!=null)
i.close();
}
}
}
*/
}
Is it possible to get updated information through several InputStreams from the same connection?
Thanks
Marco