I have tested your code in order to fetch the contents of a page on C5, on a software version released on 11th December 2010 and I didn't have any problems.
This is what I tried
Code:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Temp extends MIDlet {
Form f;
HttpConnection httpConnection;
StringBuffer strBuf;
String dataBuffer;
public Temp() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected String readServerData() {
try{
httpConnection = (HttpConnection) Connector.open("http://www.google.com");
InputStream inputStream=httpConnection.openInputStream();
strBuf = new StringBuffer();
int length = (int) httpConnection.getLength();
byte incomingData[] = null;
if (length != -1) {
incomingData = new byte[2048];
while ((length = inputStream.read(incomingData)) != -1) {
strBuf.append(new String(incomingData, 0, length, "UTF-8"));
}
dataBuffer = strBuf.toString();
} else
{
ByteArrayOutputStream byteStream =
new ByteArrayOutputStream();
int ch;
//reading the stream byte by byte to prevent the
//loss of data that is sometimes observed on reading all at once.
while ((ch = inputStream.read()) != -1) {
byteStream.write(ch);
}
dataBuffer = new String(byteStream.toByteArray(), "UTF-8");
byteStream.close();
}
inputStream = null;
strBuf = null;
incomingData = null;
return dataBuffer;
}catch(Exception e){return null;}
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
String output;
output=readServerData();
f=new Form("");
f.append(output);
Display.getDisplay(this).setCurrent(f);
}
}
Can you try the full example as I post it here? Can you get the message of the Exception? You can see what software version you are testing on by pressing *#0000# and update your device's software, if necessary.