Firmware: V 2.54, 2-3-2003
Network: AT&T
Using the code from the networking example at:
http://wireless.java.sun.com/midp/articles/network/
(Example 3)
Repeating the connection in a loop we see the following replicable behavior:
1. Network requests work fine till around the 13th interation.
2. This request tends to throw an exception. (i.e. unexpected end-of-stream encounted)
3. We believe this exception is caused by a network timeout based on our other tests.
4. The 14th request is then successful but the 15th request also times out.
5. At this point we appear to have lost the ability to do any net connection as our 16th, 17th, 18th, 19th, and 20th requests all throw exceptions.
Code Follows:
package thirdexample;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
/**
* An example MIDlet to invoke a CGI script.
*/
public class ThirdExample extends MIDlet
{
private Display display;
String url = "http://developer.java.sun.com/cgi-bin/getgrade.cgi?name=182016";
private Form fForm;
public ThirdExample()
{
display = Display.getDisplay(this);
}
/**
* Initialization. Invoked the MIDlet is activated.
*/
public void startApp()
{
fForm = new Form("Test");
display.setCurrent(fForm);
for (int i = 0; i < 20; i++)
{
fForm.append("try #" + (i + 1) + ": ");
try
{
long time1 = System.currentTimeMillis();
getGrade(url);
long time2 = System.currentTimeMillis();
fForm.append("(" + (time2 - time1) + " millis)\n");
}
catch (IOException e)
{
fForm.append(e.getMessage());
e.printStackTrace();
}
}
}
/**
* Pause, discontinue ....
*/
public void pauseApp() { }
/**
* Destroy must cleanup everything.
*/
public void destroyApp(boolean unconditional) { }
/**
* Retrieve a grade....
*/
void getGrade(String url) throws IOException
{
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer b = new StringBuffer();
TextBox t = null;
try
{
c = (HttpConnection) Connector.open(url);
c.setRequestMethod(HttpConnection.GET);
c.setRequestProperty("IF-Modified-Since",
"10 Nov 2000 17:29:12 GMT");
c.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Confirguration/CLDC-1.0");
c.setRequestProperty("Content-Language",
"en-CA");
os = c.openOutputStream();
/**
String str = "?idnum=182016";
byte postmsg[] = str.getBytes();
for(int i=0;i<postmsg.length;i++) {
os.writeByte(postmsg[i]);
}
os.flush();
*/
is = c.openDataInputStream();
int ch;
while ((ch = is.read()) != -1)
{
b.append((char) ch);
}
//t = new TextBox("Final Grades", b.toString(), 1024, 0);
fForm.append(b.toString());
}
finally
{
if (is != null)
{
is.close();
}
if (os != null)
{
os.close();
}
if (c != null)
{
c.close();
}
}
//display.setCurrent(t);
}
}

Reply With Quote

