×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    17

    HTTP connection to Tomcat Server

    Hi,

    Following is some basic code I use to connect to a Tomcat server from a MIDlet(Excuse the lack of tab layout, its not been copied correctly):

    public SyncLogic2(String userName, String Password) throws Exception
    {
    password = Password;
    try
    {
    System.out.println("Attempting to open syncfile:"+userName+".txt. Using password: "+password);
    //conn = (StreamConnection)Connector.open("http://localhost:8080/smartorg/"+userName);
    conn = (StreamConnection)Connector.open("http://62.255.76.7:8080/smartorg/"+userName+".txt");
    if (conn==null)
    {
    System.out.println("Conn is null");
    throw new Exception("SyncLogic2 constructor(): Conn is null");
    }
    in = conn.openDataInputStream();
    if (in==null)
    {
    System.out.println("IN is null");
    throw new Exception("SyncLogic2 constructor(): In is null");
    }
    }

    catch(IOException e)
    {
    System.out.println("SyncLogic2 constructor(): EXCEPTION: could not open HTTP connection: "+e.getMessage());
    throw new Exception("SyncLogic2 constructor(): EXCEPTION: could not open HTTP connection: "+e.getMessage());
    }
    }

    *******************************************

    Notice the two string containing the URL. The first one is REMMED out and contains "localhost" written for the emulator which runs fine.
    The active line contains the IP address of the Tomcat Server. This also runs fine on the emulator. However when the system is packaged and transferred to the phone, when this class is accessed via a function in th GUI, the phone simply ignores the function. When I EXIT my application, THEN the I see the phone attempting to make a data call, which always fails after 12 seconds.

    Anyone who can help me??? I have no idea whats causing it.

    Please help!

    chris
    Last edited by chrisnsx; 2003-04-11 at 21:36.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    There's not attempt to read the resulting input stream. Have you tried the same with an:

    in.read ();

    in there somewhere?

    --lee

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    17
    Sorry, I didnt include all my code. You can see from the method blocks that most of the variables and objects(including "in") were initialised outside the method block(as shown below). Hence their scope persists so that the following methods (below) can access them. The above method is located in the following class.
    startSyncNow() starts the thread that actually reads the text file from the http connection. This makes calls to getNextLine() which returns a line from the file at a time.

    As I said it works fine in the emulator, reading and decoding the file. Thanks for the input anyway, maybe you can help me further?

    prety plz with sugar on top?


    class SyncLogic2
    {
    StreamConnection conn = null;
    DataInputStream in = null;
    //Reader in = null;
    StringBuffer data = new StringBuffer();
    int ch;
    String currentLine;
    SyncLogic syncLogic = new SyncLogic();
    String password;

    private String getNextLine()
    {
    try
    {

    while ((ch = in.read()) != -1) //While inputstream has not reached end of file
    {
    if (ch != '\n') //If inputstream has not encountered a carriage return
    {
    data.append((char)ch);
    }
    else //Else return the line of data, ch = /n
    {
    String returnString = data.toString();
    data = new StringBuffer(); //Clear the Stringbuffer
    return returnString;
    }
    }
    }
    catch(IOException e)
    {
    System.out.println("SyncLogic2.getNextLine(): EXCEPTION: Error obtaining next line of syncfile: "+e.getMessage());
    }
    return null;
    }

    public LastSyncObject startSyncNow() throws Exception
    {
    System.out.println("Waypoint 11");
    String currentLine = "";
    String date = "";
    String time = "";
    int numMemos = 0;
    int numTodos = 0;
    int numContacts = 0;
    int numSchedules =0;
    Vector vecNewMemos = new Vector();
    Vector vecNewTodos = new Vector();
    Vector vecNewContacts = new Vector();
    Vector vecNewSchedules = new Vector();
    DBControl db;
    System.out.println("Waypoint 12");

    currentLine = getNextLine();
    System.out.println("Waypoint 13");
    if (currentLine==null) System.out.println("currentLine is null.");

    if (!currentLine.startsWith(password, 0)) throw new Exception("Password Incorrect");
    getNextLine();
    currentLine = getNextLine();

    while (!currentLine.startsWith("endoffile", 0))
    {
    System.out.println("currentLine = "+currentLine);

    //System.out.println("SyncLogic2: Inside 1st while loop, retrieved line as:"+currentLine+".");
    if (currentLine.startsWith("memos",0))
    {
    System.out.println("SyncLogic2: Entered memo section by detecting string memos as:"+currentLine);


    etc etc etc etc
    Last edited by chrisnsx; 2003-04-13 at 23:49.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Same Problme

    Hi, have you solved your problem. It looks like I have nearly the same !?!?!

    best regrads

    Joerg

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    22
    hey dude,i think u got it wrong.
    as u r using GET method so u have to send the value along with the variable
    eg:
    conn=http://localhost:8080/servlet?"username="usrname+"password="passwd;
    now the servlet will take care of the values that u r passing using
    HttpResponse
    There will be no problem in that.
    in case of any problem u can mail me at sujoydas1981@hotmail.com
    best of luck.

Posting Permissions

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