Skip to main content

HttpConnection and data download problem on Nokia 6680

1 reply [Last post]
Anonymous

Hi all,

I am trying to download some data from a web application on my j2me
application. On server side the data is written in byte array using
following code.
I am wring the byte array in the OutputStream. This array is created using
ByteArrayOutputStream>>>toByteArray. The first element of the array is the
int .. it is written using DataOutputStream.writeInt().

following is my code on Mobile client
public void downloadCategories(final String URL) {
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>");
HttpConnection con = null;
InputStream in = null;
DataInputStream din = null;
String message = null;
ByteArrayOutputStream bout = null;
DataOutputStream dout = null;
byte[] data = null;
byte[] testArray = new byte[13];
int size = 0;
setCatInputStrm(null);
try {
try {
System.out.println("con is : "+con);
con = (HttpConnection) Connector.open(URL);

System.out.println(" con after is : "+con);
if(con == null){
initPage(PAGE_CONNECTION_ERROR);
}
con.setRequestMethod(HttpsConnection.GET);
} catch (Exception err) {
initPage(PAGE_CONNECTION_ERROR);

err.printStackTrace();
return;
}
in = con.openInputStream();

// suchit testing
// int len = (int)con.getLength();
// if (len > 0) {
// int actual = 0;
// int bytesread = 0 ;
// byte[] testdata = new byte[len];
// while ((bytesread != len) && (actual != -1)) {
// actual = in.read(testdata, bytesread, len
-bytesread);
// bytesread += actual;
// System.out.println("length is : "+len+" done :
"+bytesread);
// }
// ByteArrayInputStream bin = new
ByteArrayInputStream(testdata);
// din = new DataInputStream(bin);
// } else {
// int ch;
// while ((ch = in.read()) != -1) {
// System.out.println("in other try");
// }
// }
//
//
//// suchit testing ends

din = new DataInputStream(in);
System.out.println("available is : "+din.available());
size = din.readInt();
testArray = new byte[size];
din.readFully(testArray);
setCatInputStrm(testArray);
} catch (Exception ex) {
try {
ex.printStackTrace();
System.out.println("error making connection >> con
is "+ ex.getMessage());
setError_string("" + ex.getMessage());
String errorStr = "Error is : " + ex+" test Array
size is : "+testArray.length+" in : "+in+" din : "+din+" size is :
"+size+" available : "+din.available();
ErrorForm erForm = new ErrorForm(errorStr);
mid.display.setCurrent(erForm);
// initPage(PAGE_CONNECTION_ERROR);
} catch (Exception e) {
e.printStackTrace();
}
// ex.printStackTrace();
}

finally {

try {
if (con != null)
con.close();

if (in != null)
in.close();

// if(din !=null)
// din.close();
} catch (IOException ex) {
ex.printStackTrace();
}

}

}
}

);
t.start();
try {
t.join();
} catch (InterruptedException ex) {
System.out.println("error is here");
ex.printStackTrace();
}
}

when i try it on wtk emulator it works fine and downloads the data. But when
put on Nokia 6680. It displays the exception form with EOFException at
din.readInt()...with inputStream length as -1...InputStream.available as -1.

Kindly suggest me any solution for this.

Thanking all
-Suchit

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Zyl, Alexander

Hi,

You need to determine content length on server

Best,
Alexander Zyil

-----Original Message-----
From: A mailing list for KVM discussion
[mailto:KVM-INTEREST@JAVA.SUN.COM] On Behalf Of Suchit Tatkare
Sent: Friday, March 02, 2007 4:16 PM
To: KVM-INTEREST@JAVA.SUN.COM
Subject: HttpConnection and data download problem on Nokia 6680

Hi all,

I am trying to download some data from a web application on my j2me
application. On server side the data is written in byte array using
following code.
I am wring the byte array in the OutputStream. This array is created
using
ByteArrayOutputStream>>>toByteArray. The first element of the array is
the
int .. it is written using DataOutputStream.writeInt().

following is my code on Mobile client
public void downloadCategories(final String URL) {
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println("<<<<<<<<<<<<<<<< Thread>>>>>>>>>>>>>>>>>");
HttpConnection con = null;
InputStream in = null;
DataInputStream din = null;
String message = null;
ByteArrayOutputStream bout = null;
DataOutputStream dout = null;
byte[] data = null;
byte[] testArray = new byte[13];
int size = 0;
setCatInputStrm(null);
try {
try {
System.out.println("con is : "+con);
con = (HttpConnection) Connector.open(URL);

System.out.println(" con after is : "+con);
if(con == null){
initPage(PAGE_CONNECTION_ERROR);
}
con.setRequestMethod(HttpsConnection.GET);
} catch (Exception err) {
initPage(PAGE_CONNECTION_ERROR);

err.printStackTrace();
return;
}
in = con.openInputStream();

// suchit testing
// int len = (int)con.getLength();
// if (len > 0) {
// int actual = 0;
// int bytesread = 0 ;
// byte[] testdata = new byte[len];
// while ((bytesread != len) && (actual != -1)) {
// actual = in.read(testdata, bytesread, len
-bytesread);
// bytesread += actual;
// System.out.println("length is : "+len+"
done :
"+bytesread);
// }
// ByteArrayInputStream bin = new
ByteArrayInputStream(testdata);
// din = new DataInputStream(bin);
// } else {
// int ch;
// while ((ch = in.read()) != -1) {
// System.out.println("in other try");
// }
// }
//
//
//// suchit testing ends

din = new DataInputStream(in);
System.out.println("available is :
"+din.available());
size = din.readInt();
testArray = new byte[size];
din.readFully(testArray);
setCatInputStrm(testArray);
} catch (Exception ex) {
try {
ex.printStackTrace();
System.out.println("error making connection >>
con
is "+ ex.getMessage());
setError_string("" + ex.getMessage());
String errorStr = "Error is : " + ex+" test
Array
size is : "+testArray.length+" in : "+in+" din : "+din+" size is :
"+size+" available : "+din.available();
ErrorForm erForm = new ErrorForm(errorStr);
mid.display.setCurrent(erForm);
// initPage(PAGE_CONNECTION_ERROR);
} catch (Exception e) {
e.printStackTrace();
}
// ex.printStackTrace();
}

finally {

try {
if (con != null)
con.close();

if (in != null)
in.close();

// if(din !=null)
// din.close();
} catch (IOException ex) {
ex.printStackTrace();
}

}

}
}

);
t.start();
try {
t.join();
} catch (InterruptedException ex) {
System.out.println("error is here");
ex.printStackTrace();
}
}

when i try it on wtk emulator it works fine and downloads the data. But
when
put on Nokia 6680. It displays the exception form with EOFException at
din.readInt()...with inputStream length as -1...InputStream.available as
-1.

Kindly suggest me any solution for this.

Thanking all
-Suchit

========================================================================
===
To unsubscribe, send email to listserv@java.sun.com and include in the
body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff KVM-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".