File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java Micro Edition and the fly likes J2me:Question about HttpConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "J2me:Question about HttpConnection" Watch "J2me:Question about HttpConnection" New topic
Author

J2me:Question about HttpConnection

tom cai
Greenhorn

Joined: Nov 13, 2003
Posts: 1
i am using HttpConnection to download data from servlet into my palm device,
there are 666 records in the server,and i can download 15 records one time,so i need to connect 45 times to download all the records.at the beginning,i works well,but when it works 13 times,a java.io.exception is thrown out afer i call the function HttpConnection.getInputStream(),the palm device alerts that it can't connect to the destnation URL.I have run softick ppp and HotSync before the connection was open.the soure code is:

public String connect(String data)
{
String strResult="";
try
{

if (conn ==null)
{
System.out.println("conn is null") ;
}
conn=(HttpConnection)Connector.open(url) ;
System.out.println("create new connection success!") ;

conn.setRequestMethod(HttpConnection.POST) ;
conn.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0") ;
conn.setRequestProperty("Content-type","application/x-www-form-urlencoded") ;

try
{
os = conn.openDataOutputStream();
}catch(Exception ex)
{
System.out.println("os open failed!") ;
buildError("connect","os open failed:"+ex.toString() );
}


data=PubFun.convert2Unicode(data) ;
os.write(data.getBytes() );
try
{
is = conn.openDataInputStream();
}catch(Exception ex)
{
System.out.println("is open failed!") ;
buildError("connect","is open failed:"+ex.toString() );
}

String contentType=conn.getType() ;
byte[] temp;
int len=(int)conn.getLength() ;
if (len>0)
{
temp=new byte[len];
int ch;
for (int i=0;i<len;i++)
{
temp[i]=(byte)is.read() ;
}

}
else {
temp=new byte[1024*8];
int ch;
len=0;
while((ch=is.read())!=-1){
temp[len++]=(byte)ch;
}
}
strResult=new String(temp);
strResult=strResult.trim() ;

}catch(IOException e)
{
System.out.println("connect failed!"+e.toString()) ;
buildError("connect",e.toString() );
System.out.println(" has build error1:"+this.mErrors .getFirstError() ) ;

}
catch(Exception e)
{
System.out.println("connect failed!"+e.toString()) ;
buildError("connect",e.toString() );
System.out.println(" has build error1:"+this.mErrors .getFirstError() ) ;
}
finally{

try
{
is.close() ;
System.out.println("is was closed!") ;
is=null;
}
catch(Exception ex)
{
System.out.println("close is") ;
buildError("close is",ex.toString() );
}
try
{
os.close() ;
System.out.println("os was closed!") ;
os=null;
}
catch(Exception ex)
{
System.out.println("close os") ;
buildError("close os",ex.toString() );
}
try
{
conn.close() ;
System.out.println("conn was closed!") ;
conn=null;
}
catch(Exception ex)
{
System.out.println("close conn") ;
buildError("close conn",ex.toString() );
}

System.out.println("before return:"+strResult) ;
return strResult;
}

thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: J2me:Question about HttpConnection
 
Similar Threads
IOException on outputStream close on Nokia
sending image to server using HTTP connection
problem in converting string to byte array.
URLConnection problem
httpConnection limted?