• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic