wood burning stoves 2.0*
The moose likes Java Micro Edition and the fly likes what is wrong with this?-HTML source, not the normal content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "what is wrong with this?-HTML source, not the normal content" Watch "what is wrong with this?-HTML source, not the normal content" New topic
Author

what is wrong with this?-HTML source, not the normal content

weishan zhang
Greenhorn

Joined: Sep 25, 2002
Posts: 8
Hi all,
I am running an example called HttpView from SunOne Studio. Here is part of the code. Somehow the emulator shows the HTML source code, instead of normal content. Would someone please help to see what is wrong with the code. Thanks a lot in advance!
public void run() {
long start = 0, end = 0;
int bytecode_count_start = 0, bytecode_count_end = 0;
Thread mythread = Thread.currentThread();
String method = HttpConnection.GET;
if (requestCommand == headCommand) {
method = HttpConnection.HEAD;
} else if (requestCommand == postCommand) {
method = HttpConnection.POST;
}
if (content == null) {
content = new TextBox("Content", "", 4096, 0);
content.addCommand(backCommand);
content.addCommand(headersCommand);
content.setCommandListener(this);
}
// Clear the buffers and forms so then can be displayed
// even if an exception terminates reading early.
content.setTitle("Body len = 0");
content.setString("");
genErrorsForm("Errors", null);
clearForm(requestForm);
clearForm(headerForm);
progressGauge.setValue(1);
HttpConnection conn = null;
InputStream input = null;
OutputStream output = null;
StringBuffer b;
String string = null;
try {
long len = 0;

conn = (HttpConnection)Connector.open(url);
conn.setRequestMethod(method);
setConfig(conn);
if (mythread != thread) {
return;
}
progressGauge.setValue(2);
for (int hops=0; hops<2; hops++) {
// Send data to the server (if necessary). Then, see if
// we're redirected. If so, hop to the new URL
// specified by the server.
//
// You can choose how many hops to make by changing the
// exit condition of this loop.
//
// To see an example of this, try the link
// "http://www.sun.com/products" link, which will
// redirect you to a link with a session ID.
if (method == HttpConnection.POST) {
System.out.println("run method"+method);
output = conn.openOutputStream();
if (mythread != thread) {
return;
}
output.write("hello midlet world".getBytes());
output.close();
output = null;
}
HttpConnection newConn = handleRedirects(conn);
if (conn != newConn) {
conn = newConn;
} else {
break;
}
}
genRequestForm(conn);

// System.out.println("commandAction url"+url);
System.out.println("commandAction conn"+conn);
input = conn.openInputStream();
if (mythread != thread) {
return;
}
content.setTitle(conn.getResponseMessage() +
" (" + conn.getResponseCode() + ")");
genHeaderForm(conn);
progressGauge.setValue(5);
if (mythread != thread) {
return;
}
// Download the content of the URL. We limit our download
// to 4096 bytes (content.getMaxSize()), as most small
// devices may not be able to handler larger size.
//
// A "real program", of course, needs to handle large
// downloads intelligently. If possible, it should work
// with the server to limit downloads to small sizes. If
// this is not possible, it should download only part of
// the data and allow the user to specify which part to
// download.
len = conn.getLength();
b = new StringBuffer(len >= 0 ? (int)len : 1000);
int max = content.getMaxSize();
if (len != -1) {
// Read content-Length bytes, or until max is reached.
int ch = 0;
for (int i = 0; i < len; i++) {
if ((ch = input.read()) != -1) {
if (ch <= ' ') {
ch = ' ';
}
b.append((char) ch);
if (b.length() >= max) {
break;
}
}
}
} else {
// Read til the connection is closed, or til max is reached.
// (Typical HTTP/1.0 script generated output)
int ch = 0;
len = 0;
while ((ch = input.read()) != -1) {
if (ch <= ' ') {
ch = ' ';
}
b.append((char)ch);
if (b.length() >= max) {
break;
}
}
}

string = b.toString();
// System.out.print("DATA: [");
// System.out.print(string);
// System.out.println("]");


if (mythread != thread) {
return;
}
progressGauge.setValue(8);
content.setTitle("Body len = " + b.length());

if (b.length() > 0) {
content.setString(string);
} else {
content.setString("no data");
}
display.setCurrent(content);
progressGauge.setValue(9);
} catch (OutOfMemoryError mem) {
// Mmm, we still run out of memory, even after setting
// max download to 4096 bytes. Tell user about the error.
//
// A "real program" should decide on the max download
// size depending on available heap space, or perhaps
// allow the user to set the max size
b = null;
content = null; // free memory to print error
System.out.println("Out of Memory");
mem.printStackTrace();
if (mythread != thread) {
genErrorsForm("Memory", mem);
display.setCurrent(errorsForm);
}
} catch (Exception ex) {
ex.printStackTrace();
genErrorsForm("Errors", ex);
display.setCurrent(errorsForm);
} finally {
cleanUp(conn, input, output);
if (mythread == thread) {
progressGauge.setValue(10);
}
}
}
Liam Quinn
Ranch Hand

Joined: Jan 18, 2002
Posts: 35
Your code isn't doing anything to handle the HTML. If you want to render the HTML, you can use the ReqwirelessWeb library:
http://www.reqwireless.com/products-web.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what is wrong with this?-HTML source, not the normal content
 
Similar Threads
J2me:Question about HttpConnection
the problem abt communication between serlvet & midlet
Problem while building session with doPost method (at server side)
J2MEWTK 1.0.3 HTTP Connection
Help: Potential deadlock in commandAction() ?