×

Discussion Board

Results 1 to 12 of 12
  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    28

    MIDP applicatyion accessing a servlet not working in series 60 emulator

    HI,

    I have developed an application which access a servlet in Tomcar server. The application works in j2me emulator and nokia 40 series emulator but when i try to run ot on a series 60 emulator

    i am getting the following error




    Nokia Series 60 MIDP Concept SDK Beta 0.2: disconnect!! httpver: HTTP/1.1

    Nokia Series 60 MIDP Concept SDK Beta 0.2: disconnect!! HTTP/1.1 !!!!!!!

    Please som one help me to solve the problem

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    393
    Please attach/post your code (client and server) so that we can help you more efficiently.

    [N]/Forum Nokia

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    28
    I am posting my code here for the midlet which access the servlet

    The midlet works fine in series 40 emulator but gives an error in series 60 gives error

    Nokia Series 40 MIDP Concept SDK Beta 0.3: Emulator command: F:\Nokia\Devices\Nokia_Series_40_MIDP_Concept_SDK_Beta_0_3\bin\midp -classpath F:\Nokia\Devices\Nokia_Series_40_MIDP_Concept_SDK_Beta_0_3\lib\classes.zip;F:\WTK20\apps\nokiatester\bin\nokiatester.jar -Xdescriptor F:\WTK20\apps\nokiatester\bin\nokiatester.jad -DcommandPort=1180 -DmirrorPort=1181

    Nokia Series 60 MIDP Concept SDK Beta 0.2: Emulator command: F:\Nokia\Devices\Nokia_Series_60_MIDP_Concept_SDK_Beta_0_2\bin\midp -heapsize 8M -classpath F:\Nokia\Devices\Nokia_Series_60_MIDP_Concept_SDK_Beta_0_2\lib\classes.zip;F:\WTK20\apps\nokiatester\bin\nokiatester.jar -Xdescriptor F:\WTK20\apps\nokiatester\bin\nokiatester.jad -DcommandPort=1685 -DmirrorPort=1686

    Nokia Series 60 MIDP Concept SDK Beta 0.2: StreamConnection.connect: proxy: null

    Nokia Series 60 MIDP Concept SDK Beta 0.2: sending request:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: props: IF-Modified-Since: 20 Jan 2001 16:19:14 GMT

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: sending request:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: props: User-Agent: Profile/MIDP-1.0 Configuration/CLDC-1.0

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: sending request:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: props: Content-Language: en-CA

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: sending request:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: props: Content-Length: 0

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: sending request:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: props: Host: localhost:8080

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2:

    Nokia Series 60 MIDP Concept SDK Beta 0.2: 111

    Nokia Series 60 MIDP Concept SDK Beta 0.2: disconnect!! httpver: HTTP/1.1

    Nokia Series 60 MIDP Concept SDK Beta 0.2: disconnect!! HTTP/1.1 !!!!!!!





    -----------------code-----------

    import java.io.*;
    import javax.microedition.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;


    public class FirstMidletServlet extends MIDlet {

    private Display display;

    String url = "http://localhost:8080/midp/hello";

    public FirstMidletServlet() {
    display = Display.getDisplay(this);
    }

    /**
    * Initialization. Invoked when we activate the MIDlet.
    */
    public void startApp() {
    try {
    invokeServlet(url);
    } catch (IOException e) {
    System.out.println("IOException " + e);
    e.printStackTrace();
    }
    }

    /**
    * Pause, discontinue ....
    */
    public void pauseApp() {

    }

    /**
    * Destroy must cleanup everything.
    */
    public void destroyApp(boolean unconditional) {
    }

    /**
    * Prepare connection and streams then invoke servlet.
    */

    void invokeServlet(String url) throws IOException {
    HttpConnection c = null;
    InputStream is = null;
    StringBuffer b = new StringBuffer();
    TextBox t = null;
    try {
    c = (HttpConnection)Connector.open(url);
    c.setRequestMethod(HttpConnection.GET);
    c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
    c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
    c.setRequestProperty("Content-Language", "en-CA");
    is = c.openDataInputStream();
    int ch;
    while ((ch = is.read()) != -1) {
    b.append((char) ch);
    //System.out.println((char)ch);
    }
    t = new TextBox("First Servlet", b.toString(), 1024, 0);
    } finally {
    if(is!= null) {
    is.close();
    }
    if(c != null) {
    c.close();
    }
    }
    display.setCurrent(t);
    }
    }

  4. #4
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    393
    I get those messages too. I think it is just debug information because the midlet successfully performs a POST to the servlet and gets the response in my case. I tested with the Sum example.

    Is your concern just the messages or you are not able to connect to the servlet using the Series 60 emulator?

    [N]/Forum Nokia

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    28
    Hi nmittal,
    Thanks for your help.


    My concern is that some times i am not getting a response back from server. sometimes i get a response only after 3 rd click..

    ie for example.. I have a list in the applications. if i select an item from the list and click the selection. This action is supposed to get data from the server. on first attemp i am not getting any result.but if the select the same option second time.it connects to the server and fetches the data.


    My concern is that i am facing this problem only in nokia emulator(in series 40 and series 60) but it works well on a j2me wireless tool kit emulator.

  6. #6
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    393
    It's highly recommended that the HTTP connection should be created in a separate thread. You should close the connection and streams in the finally block.

    Doing this would keeps the UI responsive while the networking is handled in a separate thread.

    [N]/Forum Nokia

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    28
    Hi nmittal,

    I hadled the httpconnection in a different thread, still i face the same problem. My concern is that it works perfectly in sun wireless kit emulator.but gives trouble only in nokia 40 and 60 series emulator

  8. #8
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    393
    Best would be to try on the real phone. The S60 and S40 are concept emulators and do not simulate the phones.

    Please post your complete code for us to examine further.

    [N]/Forum Nokia

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    28
    Hi nmittal,
    i have posted below the code with HTTP handling in a separate thread

    import javax.microedition.rms.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import java.io.*;
    import java.util.*;
    public class Precuationmidlet extends MIDlet implements CommandListener
    {

    List menu=null;
    static final Command FindCommand = new Command("Find", Command.OK,1);
    static final Command exitCommand = new Command("Exit", Command.STOP,2);
    static final Command backCommand = new Command("Back", Command.STOP,2);
    static final Command selectCommand = new Command("Choose", Command.OK,1);

    TextBox input=null;
    Display display=null;
    public Precuationmidlet(){}
    public void startApp() throws MIDletStateChangeException{
    display=Display.getDisplay(this);
    input=new TextBox("Brand Name","",15, TextField.ANY);
    input.addCommand(FindCommand);
    input.addCommand(exitCommand);
    input.setString("");
    input.setCommandListener(this);
    display.setCurrent(input);
    }
    public void pauseApp() { }
    public void destroyApp(boolean unconditional) {
    notifyDestroyed();
    }
    public void commandAction(Command c,Displayable d){
    String label = c.getLabel();
    if(label.equals("Exit")) {
    destroyApp(true);
    }
    if (label.equals("Find"))
    {
    String user = input.getString();
    callServlet call=new callServlet(user,"http://localhost:8080/midp/jdbc");
    call.start();
    }


    }

    public class callServlet extends Thread implements CommandListener
    {
    String name=null;
    String url=null;
    public callServlet(String name,String url){
    this.name=name;
    this.url=url;

    }
    public void run(){
    try{
    displaymenu();
    }catch(IOException ex){}


    }
    public void displaymenu() throws IOException{

    HttpConnection c = null;
    InputStream is = null;
    OutputStream os = null;
    StringBuffer b = new StringBuffer();
    String str1="";
    String str2="";
    String result="";
    String brandname="";
    menu=new List("Brands", Choice.IMPLICIT);
    //System.out.println(url);
    //System.out.println(name);
    try {
    c = (HttpConnection)Connector.open(url);
    c.setRequestMethod(HttpConnection.POST);
    //c.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT");
    c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
    c.setRequestProperty("Content-Language", "en-CA");
    // send request to the servlet.
    os = c.openOutputStream();
    String str =name;
    byte postmsg[] = str.getBytes();
    System.out.println("Length: "+str.getBytes());
    for(int i=0;i<postmsg.length;i++) {
    os.write(postmsg[i]);
    System.out.println(postmsg[i]);
    }

    os.flush();
    // receive response and display it in a textbox.
    is=null;
    //b=null;
    is = c.openDataInputStream();
    int ch;
    ch=0;
    System.out.println(b.toString() +" b before value");
    while((ch = is.read()) != -1) {
    b.append((char) ch);
    // System.out.print((char)ch);
    }
    result=b.toString();
    //System.out.println("result value" +result);
    if (result.length()>0)
    {
    int len=result.length();
    int i=0;
    str1=result.substring(0,len);
    int strlen=str1.length();
    int j=0;
    brandname=str1.substring(0,strlen-1);

    while (j<strlen-1){
    int pos=brandname.indexOf("|");
    if (pos<=0){
    str2=brandname.substring(0,brandname.length());
    menu.append(str2,null);
    break;
    }

    str2=brandname.substring(0,pos);
    j=pos;
    //System.out.println(i);
    //System.out.println(pos);
    menu.append(str2,null);
    brandname=brandname.substring(pos+1,brandname.length());

    }

    }
    else
    {
    str1="No Data Available";
    }
    menu.addCommand(backCommand);
    menu.addCommand(selectCommand);
    menu.setCommandListener(this);
    display.setCurrent(menu);
    //System.out.println(str1);

    } finally {

    if(is!= null) {
    is.close();
    }
    if(os != null) {
    os.close();
    }
    if(c != null) {
    c.close();
    }

    }
    }
    public void commandAction(Command c,Displayable d){
    String label = c.getLabel();
    if (label.equals("Back")){
    input.setString("");
    display.setCurrent(input);
    }
    if (label.equals("Choose")){

    String brand=menu.getString(menu.getSelectedIndex());
    callServlet call=new callServlet(brand,"http://localhost:8080/midp/company");
    call.start();
    }
    }
    };
    };//main class
    Last edited by ferozekadamapuzha; 2003-05-12 at 12:39.

  10. #10
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    393

    Where's the Http connection thread?

    Hi,

    I don't see you using a separate thread for your HTTP connection in your code. You can find a sample on this forum itself if you are not sure how to do that. In your case the InvokeServet class should extend Thread or implement Runnable interface.

    [N]/Forum Nokia

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    28
    The code with httpconnection in a different thread is posted below


    please check it

    Last edited by ferozekadamapuzha; 2003-05-14 at 05:03.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    1

    Question Re: MIDP applicatyion accessing a servlet not working in series 60 emulator

    I am newbie trying to develop a mobile application to access a web application through a Servlet but the program hands up middle way. The code for the classes I am using are:-

    Sample1.java

    Code:
    
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.*;
    import java.io.*;
    
    public class Sample1 extends MIDlet implements CommandListener {
    /*
     * the default value for the URL string is
     * http://www.webyu.com/servlets/webyu/wirelessdevnetsample1
     */
        
        private static String defaultURL =
                "http://www.webyu.com/servlets/webyu/wirelessdevnetsample1";
        
    // GUI component for user to enter String
        private Display myDisplay = null;
        private Form mainScreen;
        private TextField requestField;
        
    // GUI component for displaying header information
        private Form resultScreen;
        private StringItem resultField;
        
    // the "SEND" button used on the mainScreen
        Command sendCommand = new Command("SEND", Command.OK, 1);
        
    // the "BACK" button used on the resultScreen
        Command backCommand = new Command("BACK", Command.OK, 1);
        
        public Sample1(){
            // initializing the GUI components for entering Web URL
            myDisplay = Display.getDisplay(this);
            mainScreen = new Form("Type in a string:");
            requestField =
                    new TextField(null, "GREAT ARTICLE", 100, TextField.ANY);
            mainScreen.append(requestField);
            mainScreen.addCommand(sendCommand);
            mainScreen.setCommandListener(this);
        }
        
        public void startApp() {
            myDisplay.setCurrent(mainScreen);
        }
        
        public void pauseApp() {
        }
        
        public void destroyApp(boolean unconditional) {
        }
        
        public void commandAction(Command c, Displayable s) {
            if (c == sendCommand) {
                // retrieving the String that user entered
                String requeststring = requestField.getString();
                // sending a POST request to Web server
                String resultstring = sendPostRequest(requeststring);
                // displaying the response back from Web server
                resultScreen = new Form("Result String:");
                resultField = new StringItem(null, resultstring);
                resultScreen.append(resultField);
                resultScreen.addCommand(backCommand);
                resultScreen.setCommandListener(this);
                myDisplay.setCurrent(resultScreen);
            } else if (c == backCommand) {
                // do it all over again
                requestField.setString("SOMETHING GOOD");
                myDisplay.setCurrent(mainScreen);
            }
        }
        
    // send a POST request to Web server
        public String sendPostRequest(String requeststring) {
            HttpConnection hc = null;
            DataInputStream dis = null;
            DataOutputStream dos = null;
            StringBuffer messagebuffer = new StringBuffer();
            try {
                // Open up a http connection with the Web server
                // for both send and receive operations
                hc = (HttpConnection)
                Connector.open(defaultURL, Connector.READ_WRITE);
                // Set the request method to POST
                hc.setRequestMethod(HttpConnection.POST);
                // Send the string entered by user byte by byte
                dos = hc.openDataOutputStream();
                byte[] request_body = requeststring.getBytes();
                for (int i = 0; i <request_body.length; i++) {
                    dos.writeByte(request_body[i]);
                }
                dos.flush();
                dos.close();
                // Retrieve the response back from the servlet
                dis = new DataInputStream(hc.openInputStream());
                int ch;
                // Check the Content-Length first
                long len = hc.getLength();
                if(len!=-1) {
                    for(int i = 0;i<len;i++)
                        if((ch = dis.read())!= -1)
                            messagebuffer.append((char)ch);
                } else {
                    // if the content-length is not available
                    while ((ch = dis.read()) != -1)
                        messagebuffer.append((char) ch);
                }
                dis.close();
            }
            
            catch (IOException ioe) {
                messagebuffer = new StringBuffer("ERROR!");
            } finally {
                // Free up i/o streams and http connection
                try {
                    if (hc != null) hc.close();
                } catch (IOException ignored) {}
                try {
                    if (dis != null) dis.close();
                } catch (IOException ignored) {}
                try {
                    if (dos != null) dos.close();
                } catch (IOException ignored) {}
            }
            return messagebuffer.toString();
        }
    }
    EnterpriseServletExample.java
    Code:
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class EnterpriseServletExample extends HttpServlet {
        public void service(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            System.out.println(
                    "Accepting data from a J2ME client. IP="+request.getRemoteAddr());
            System.out.println("Request Method...:"+request.getMethod());
            printParameterInfo(request);
            printHeaderInfo(request);
            printCookieInfo(request);
            processCounter(request);
            String data = processDataInput(request);
            String responseString =
                    "Echo \"" + data + "\" sent via " + request.getMethod();
            processResponse(response, responseString);
        }
        private void printParameterInfo(HttpServletRequest request){
            System.out.println("Parameter Info:");
            Enumeration e = request.getParameterNames();
            while (e.hasMoreElements()){
                String name = (String)e.nextElement();
                System.out.println("Parameter........:"+name+"="+request.getParameter(name));
            }
        }
        private void printHeaderInfo(HttpServletRequest request){
            System.out.println("Header Info:");
            Enumeration e = request.getHeaderNames();
            while (e.hasMoreElements()){
                String name = (String)e.nextElement();
                System.out.println("Header...........:"+name+"="+request.getHeader(name));
            }
        }
        private void printCookieInfo(HttpServletRequest request){
            System.out.println("Cookies:");
            Cookie[] cookies = request.getCookies();
            for (int ccnt=0; ccnt < cookies.length; ccnt++){
                System.out.println(
                        " cookie...........:"+cookies[ccnt]);
                System.out.println(
                        " cookie name......:"+cookies[ccnt].getName());
                System.out.println(
                        " cookie value.....:"+cookies[ccnt].getValue());
                System.out.println(
                        " cookie max age...:"+cookies[ccnt].getMaxAge());
            }
        }
        private void processCounter(HttpServletRequest request){
            HttpSession session = request.getSession(true);
            Integer sessionCounter = null;
            int tempCounter = 0;
            Object obj = session.getAttribute("hit_counter");
            if (obj == null){
                sessionCounter = new Integer(1);
            } else{
                sessionCounter = (Integer)obj;
            }
            tempCounter = sessionCounter.intValue();
            System.out.println("Hit Counter........:"+tempCounter);
            tempCounter++;
            sessionCounter = new Integer(tempCounter);
            session.setAttribute("hit_counter", sessionCounter);
        }
        private String processDataInput(HttpServletRequest request)
        throws IOException {
            int len = request.getContentLength()+2;
            String s = "";
            System.out.println("Request Content Length = "+len);
            if (len > 2) {
                System.out.println("Reading data from request:");
                BufferedReader reader = request.getReader();
                char[] buffer = new char[len];
                int i = reader.read(buffer, 0, buffer.length);
                s = new String(buffer);
                s = s.substring(0, s.length()-2);
                System.out.print(" Data.............:");
                System.out.println(s);
                System.out.println(" Data Length .....:"+i);
            }
            return s;
        }
        private void processResponse(HttpServletResponse response,
                String data)
                throws IOException {
            System.out.println("Responding...");
            response.setContentType("text/plain");
            response.setContentLength(data.length()+2);
            PrintWriter writer = response.getWriter();
            writer.println(data);
            System.out.println("Response Sent");
        }
    }
    Could anyone with a clue tell me what's wrong with the code. The code were obtained from a book sample but I don't understand it too well.
    Last edited by caesarooke; 2008-05-07 at 18:13.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×