| Reply | « Previous Thread | Next Thread » |
|
Hi. Can some one tell me what is wrong with the following piece of code. Basically I am doing a HTTP POST to a url with some parameter data=sdfsdff..
But I get nothing as the server response and I know the post command has not worked as the file which i intended to post the data on has not been updated. import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; import java.util.*; import java.lang.*; /** * * @author kgabhart * @version */ public class HttpMidlet extends MIDlet implements CommandListener,Runnable { // A default URL is used. User can change it from the GUI private static String defaultURL = "http://localhost:8080/test/servlet/EchoServlet"; private String url = "http://www.leninsgodson.com/courses/pys60/php/set_text.php"; //private String url = "http://java.sun.com"; private String Vijay = null; private int rescode = 0; // Main MIDP display private Display myDisplay = null; // GUI component for entering a URL private Form requestScreen; private TextField requestField; // GUI component for submitting request private List list; private String[] menuItems; // GUI component for displaying server responses private Form resultScreen; private StringItem resultField; // the "send" button used on requestScreen Command sendCommand; // the "exit" button used on the requestScreen Command exitCommand; // the "back" button used on resultScreen Command backCommand; public HttpMidlet(){ // initialize the GUI components myDisplay = Display.getDisplay( this ); sendCommand = new Command( "SEND", Command.OK, 1 ); exitCommand = new Command( "EXIT", Command.OK, 1 ); backCommand = new Command( "BACK", Command.OK, 1 ); // display the request URL requestScreen = new Form( "Type in a URL:" ); requestField = new TextField( null, defaultURL, 100, TextField.URL ); requestScreen.append( requestField ); requestScreen.addCommand( sendCommand ); requestScreen.addCommand( exitCommand ); requestScreen.setCommandListener( this ); // select the HTTP request method desired menuItems = new String[] {"GET Request", "POST Request"}; list = new List( "Select an HTTP method:", List.IMPLICIT, menuItems, null ); list.setCommandListener( this ); // display the message received from server resultScreen = new Form( "Server Response:" ); resultScreen.addCommand( backCommand ); resultScreen.setCommandListener( this ); }//end HttpMidlet() public void startApp() { myDisplay.setCurrent( requestScreen ); }//end startApp() public void commandAction( Command com, Displayable disp ) { // when user clicks on the "send" button if ( com == sendCommand ) { myDisplay.setCurrent( list ); } else if ( com == backCommand ) { // do it all over again requestField.setString( defaultURL ); myDisplay.setCurrent( requestScreen ); } else if ( com == exitCommand ) { destroyApp( true ); notifyDestroyed(); }//end if ( com == sendCommand ) if ( disp == list && com == List.SELECT_COMMAND ) { String result; if ( list.getSelectedIndex() == 0 ) // send a GET request to server result = sendHttpGet( requestField.getString() ); else // send a POST request to server { //result = sendHttpPost( requestField.getString() ); Thread t = new Thread(this); } result = Vijay; resultField = new StringItem( null, result ); resultScreen.append( resultField ); myDisplay.setCurrent( resultScreen ); }//end if ( dis == list && com == List.SELECT_COMMAND ) }//end commandAction( Command, Displayable ) private String sendHttpGet( String url ) { HttpConnection hcon = null; DataInputStream dis = null; StringBuffer responseMessage = new StringBuffer(); try { // a standard HttpConnection with READ access hcon = ( HttpConnection )Connector.open( url ); // obtain a DataInputStream from the HttpConnection dis = new DataInputStream( hcon.openInputStream() ); // retrieve the response from the server int ch; while ( ( ch = dis.read() ) != -1 ) { responseMessage.append( (char) ch ); }//end while ( ( ch = dis.read() ) != -1 ) } catch( Exception e ) { e.printStackTrace(); responseMessage.append( "ERROR" ); } finally { try { if ( hcon != null ) hcon.close(); if ( dis != null ) dis.close(); } catch ( IOException ioe ) { ioe.printStackTrace(); }//end try/catch }//end try/catch/finally return responseMessage.toString(); }//end sendHttpGet( String ) private String sendHttpPost( )//String url ) { HttpConnection hcon = null; DataInputStream dis = null; DataOutputStream dos = null; OutputStream os = null; StringBuffer responseMessage = new StringBuffer(); // the request body String requeststring = "This is a POST."; try { // an HttpConnection with both read and write access hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE ); // set the request method to POST hcon.setRequestMethod( HttpConnection.POST ); ////////////////////////////////////*************//////////////// hcon.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0"); hcon.setRequestProperty("Accept_Language","en-US"); //Content-Type is must to pass parameters in POST Request hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //getConnectionInformation(hcon); os = hcon.openOutputStream(); String params; params = "data=sdfsdfs"; os.write(params.getBytes()); ////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ // obtain DataOutputStream for sending the request string // dos = hcon.openDataOutputStream(); // byte[] request_body = requeststring.getBytes(); // send request string to server // for( int i = 0; i < request_body.length; i++ ) { // dos.writeByte( request_body[i] ); // }//end for( int i = 0; i < request_body.length; i++ ) // obtain DataInputStream for receiving server response dis = new DataInputStream( hcon.openInputStream() ); rescode = hcon.getResponseCode(); // retrieve the response from server int ch; while( ( ch = dis.read() ) != -1 ) { responseMessage.append( (char)ch ); }//end while( ( ch = dis.read() ) != -1 ) { } catch( Exception e ) { e.printStackTrace(); responseMessage.append( "ERROR" ); } finally { // free up i/o streams and http connection try { if( hcon != null ) hcon.close(); if( dis != null ) dis.close(); if( dos != null ) dos.close(); } catch ( IOException ioe ) { ioe.printStackTrace(); }//end try/catch }//end try/catch/finally Vijay = responseMessage.toString(); return responseMessage.toString(); }//end sendHttpPost( String ) public void pauseApp() { }//end pauseApp() public void destroyApp( boolean unconditional ) { // help Garbage Collector myDisplay = null; requestScreen = null; requestField = null; resultScreen = null; resultField = null; }//end destroyApp( boolean ) public void run() { Vijay = sendHttpPost(); } }//end HttpMidlet I would really really appreciate some help here. Thanks |
|
Join Date: Dec 2005
Posts: 1,696
Location: Europe/Poland/Warsaw
Offline
Super Contributor
|
|
hi,
#1 add "Content-Length" header and write your data length there if sending POST, sometimes help #2 do not hardcode user-agent info, set it at runtime or omit it in your code (which device you are testing actually?) #3 what is response code from your server? (if any) #4 is your server gettind data correctly locally? (on localhost) regards, Peter |
| peterblazejewicz |
| View Public Profile |
| Find all posts by peterblazejewicz |
|
Try a call in the midlet's INITIALIZE method to use your httpconnection subroutine as early as possible in the program.
I have discovered (after a couple of days of hair pulling) that if I use the http in the init method, any subsequent calls work 100%. However if I only call the httpconection routine later when the user presses a command (as you do here) the "hc = (HttpConnection)Connector.open(defaultURL, Connector.READ_WRITE);" hangs forever and the program locks up, when the uses validates http use. But if I move the exact same code (as a dummy call) to the midlets init it seems to work perfectly. (on the emulator in any case.) Even if the init throws an error for whatever reason, subsequent uses are fine. This is on MIDP1.0. If anyone can explain this behaviour I will be very interested in your thoughts. Johan |
| tdkmodchip |
| View Public Profile |
| Find all posts by tdkmodchip |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Custom HTTP Header Name in POST Request | srigans1 | Symbian Networking & Messaging | 2 | 2006-08-25 07:56 |
| Http post problem on different phones | tertiusbouwer | Mobile Java Networking & Messaging & Security | 2 | 2006-05-19 17:02 |
| Http Post | maimonides | Symbian Networking & Messaging | 0 | 2005-05-17 08:49 |
| Nokia 3230 HTTP Post Problem | alpyor | General Symbian C++ | 1 | 2005-05-13 05:15 |
| Does Vodafone UK wap gateway supports HTTP POST? | jessicay | Mobile Java Networking & Messaging & Security | 2 | 2004-07-06 16:27 |