Hi
When I run http-connection on S60 emulator, the result page will be displayed as HTML tags not a real web page. Do I need to install another software on the S60 or my code need to be changed. Here is the code (code copied from another forum for test):
Code:import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; public class HttpPOST extends MIDlet implements CommandListener { // A default URL is used. User can change it from the GUI. private static String defaultURL = "http://www.google.com"; // GUI component for entering a Web URL private Display myDisplay = null; private Form mainScreen; private TextField requestField; // GUI component for displaying server responses. private Form resultScreen; private StringItem resultField; // the "send" button used on mainScreen Command sendCommand = new Command("SEND", Command.OK, 1); // the "back" button used on resultScreen Command backCommand = new Command("BACK", Command.OK, 1); public HttpPOST() { // initialize the GUI components myDisplay = Display.getDisplay(this); mainScreen = new Form("Type in a URL:"); requestField = new TextField(null, defaultURL, 100, TextField.URL); mainScreen.append(requestField); mainScreen.addCommand(sendCommand); mainScreen.setCommandListener(this); } public void startApp() { myDisplay.setCurrent(mainScreen); } public void pauseApp() { } public void destroyApp(boolean unconditional) { // help Garbage Collector Display myDisplay = null; mainScreen = null; requestField = null; resultScreen = null; resultField = null; } public void commandAction(Command c, Displayable s) { // when user clicks on the "send" button if (c == sendCommand) { GetResponse t = new GetResponse (); t.setListener(this); t.start(); } else if (c == backCommand) { // do it all over again requestField.setString(defaultURL); myDisplay.setCurrent(mainScreen); } } class GetResponse extends Thread { private CommandListener listener; public void setListener(CommandListener cl) { listener = cl; } public void run() { // retrieve the Web URL that user entered String urlstring = requestField.getString(); // send a POST request to Web server String resultstring = ""; try { resultstring = sendPostRequest(urlstring); } catch (IOException e) { resultstring = "ERROR"; } // display the message received from Web server resultScreen = new Form("POST Result:"); resultField = new StringItem(null, resultstring); resultScreen.append(defaultURL + "\n"); resultScreen.append(resultField); resultScreen.addCommand(backCommand); resultScreen.setCommandListener(listener); myDisplay.setCurrent(resultScreen); } } // send a POST request to Web server public String sendPostRequest(String urlstring) throws IOException { HttpConnection hc = null; DataInputStream dis = null; DataOutputStream dos = null; String message = ""; // the request body String requeststring = "i=QL"; try { // an HttpConnection with both read and write access hc = (HttpConnection)Connector.open(urlstring, Connector.READ_WRITE); // set the request method to POST hc.setRequestMethod(HttpConnection.POST); //Send header information for POSt to work //i m getting NullPointerException here. //hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // obtain DataOutputStream for sending the request string dos = hc.openDataOutputStream(); byte[] request_body = requeststring.getBytes(); // send request string to Web server for (int i = 0; i < request_body.length; i++) { dos.write(request_body[i]); } // flush it out dos.flush(); // obtain DataInputStream for receiving server responses dis = new DataInputStream(hc.openInputStream()); // retrieve the responses from Web server if (hc.getResponseCode() == HttpConnection.HTTP_OK) System.out.println("Response is ok."); int ch; while ((ch = dis.read()) != -1) { message = message + (char) ch; } } finally { // free up i/o streams and http connection if (hc != null) hc.close(); if (dis != null) dis.close(); if (dos != null) dos.close(); } return message; } }

Reply With Quote


