×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    7650 Http Connection Works! Source Code Example

    hi all,

    here is the full source code for http connections on the 7650.

    i too was very frustrated over the last weeks with the problem of trying to get the 7650 to retrieve data from a web server/servlet. unfortunately no one has yet posted a full example here which puzzles me greatly. not even nokia?

    i have run this on the nokia 7650 v3.12 and it seems to work quite well. watch out for the problems with POST on v3.12 and non-english settings (HTTP 1,1 instead of HTTP 1.1) as mentioned in other posts. i put this together through the night and i t seems to work fine - even if the code is a little rough in parts.

    anyway, enough blah blah. here is the code. here are the two classes.


    <<Test.java>>

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

    /*
    An example MIDlet with simple TextBox and Exit command.
    */

    public class Test extends MIDlet implements CommandListener
    {
    private Command exitCommand; // The exit command
    private Command requestCommand;
    private Command clearCommand;
    private Display display; // The display for this MIDlet
    private Object mutex = new Object();
    Test midlet;
    HttpConnector conn;
    String message = "";

    public Test() {
    try {
    conn = new HttpConnector(this, mutex);
    conn.start();
    } catch (Exception e) {
    e.printStackTrace();
    }

    display = Display.getDisplay(this);
    clearCommand = new Command("Clear Screen", Command.SCREEN, 2);
    exitCommand = new Command("Exit", Command.SCREEN, 3);
    requestCommand = new Command("Request", Command.SCREEN, 1);
    this.midlet = this;
    }

    /*
    Start up the Test MIDlet by creating the TextBox and
    associating the exit command and listener.
    */

    public void startApp() {


    TextBox t = new TextBox("Test MIDlet", "", 500, 0);
    t.addCommand(clearCommand);
    t.addCommand(exitCommand);
    t.addCommand(requestCommand);
    t.setCommandListener(this);
    display.setCurrent(t);
    }

    /*
    Pause is a no-op since there are no background activities or
    record stores that need to be closed.
    */

    public void pauseApp() {
    }

    /*
    Destroy must cleanup everything not handled by the garbage collector.
    In this case there is nothing to cleanup.
    */

    public void destroyApp(boolean unconditional) {
    }

    /*
    Respond to commands, including exit
    On the exit command, cleanup and notify that the MIDlet has been
    destroyed.
    */

    public void commandAction(Command c, Displayable s) {
    if (c == exitCommand){
    destroyApp(false);
    notifyDestroyed();
    } else if (c == requestCommand) {
    synchronized(mutex) {
    mutex.notify(); // notify the networking thread
    }
    getResponse();
    } else if (c== clearCommand){
    clearScreen();
    }
    }

    private void getResponse() {
    // better would be a thread that waits (synchronize) on the message
    clearScreen();
    logData("Downloading"
    int retries = 0;
    while (message.equals("&quot && retries++ < 100) {
    try {
    logData("."
    Thread.sleep(100);
    } catch (InterruptedException ie) {}
    }
    logData("\n"
    if (message.equals("&quot) {
    logData("No response."
    } else {
    logData("Response Extract: " + message);
    logData("Response Time: " + retries * 100 + "ms\n"
    message = "";
    }
    }


    public void clearScreen() {
    ((TextBox)display.getCurrent()).setString(""
    }

    public void logData(String s){
    if (s==null) return;
    TextBox t = (TextBox)display.getCurrent();
    if (t==null) return;
    t.setString(t.getString() + s);
    }
    }


    <<HttpConnector.java>>

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


    public class HttpConnector extends Thread {
    Test midlet;
    private Object mutex;

    public HttpConnector(Test midlet, Object mutex)
    {
    this.midlet = midlet;
    this.mutex = mutex;
    }

    public void doRequest() throws IOException {
    StringBuffer b = new StringBuffer();
    String url = "http://138.190.197.90/";
    InputStream is = null;
    HttpConnection c = null;
    String xmlDocument = "";
    try {
    long len = 0 ;
    int ch = 0;
    char tmp = ' ';

    c = (HttpConnection)Connector.open(url);
    is = c.openInputStream();
    len = c.getLength() ;
    if ( len != -1) {
    // Read exactly Content-Length bytes
    for (int i =0 ; i < len ; i++ ) {
    if ((ch = is.read()) != -1) {
    b.append((char) ch);
    //System.out.print((char) ch);
    }
    }
    }
    else {
    // Read until the connection is closed
    while ((ch = is.read()) != -1) {
    len = is.available() ;
    b.append((char) ch);
    }
    }
    xmlDocument = b.toString();
    String temp = "";

    midlet.message = xmlDocument.substring(0,100) + "\nResponse Size: " + b.length() + " bytes\n";
    //System.out.println("Result: " + temp);
    }
    catch(Exception e) {
    // error
    }
    finally {
    is.close();
    c.close();
    }
    }

    /*
    public void doRequest(){
    String uri = "http://138.190.197.91:80/";
    HttpConnection connection = null;
    InputStream is = null;

    byte data[] = new byte[100];
    long len;
    long dataRead;

    try
    {
    connection = (HttpConnection)Connector.open(uri);

    is = connection.openInputStream();

    dataRead = is.read(data, 0, data.length);

    }

    catch (Exception e){
    midlet.logData(e.toString());
    }

    finally {
    try {
    if (is != null) is.close();
    if (connection != null) connection.close();
    } catch (IOException ioe)
    {
    midlet.logData(ioe.toString());
    }
    }
    if (data!=null)
    {
    midlet.reportRequestResult(new String(data));
    }
    }
    */

    public void run() {
    while(true) {
    synchronized(mutex) {
    try {
    //System.out.println("wait here"
    mutex.wait();
    } catch(InterruptedException e) {}

    try {
    this.doRequest();
    } catch (IOException ie) {
    // error message here
    }
    }
    }
    }
    }



    well, i think that's it. thanks to the other on the forum for the various tips i used to do this (ie: sgerogia).

    regards,

    simon

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    RE: 7650 Http Connection Works! Source Code Example

    Hi, did you try accessing a http url consisting of special port numbers(e.g 8080)? My 7210 can access a normal url, but fails if the url has a port number.

    I noticed that in your code there is a remarked block which is like what I mentioned just above, so did you succeed to to that?

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    RE: 7650 Http Connection Works! Source Code Example

    hi,

    i have only tested it on 3.12 at the moment. we will be upgrading one of our phones soon to 4.39 so i cant say yet.

    sorry i dont know what the problems is that you are having yet. we are now successfully using the code here as a basis for an app that handles text and image (png) content quite well.

    simon.

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

    RE: 7650 Http Connection Works! Source Code Example

    on port other than 80, it all depent on ur mobile ISP, some GPRS/wap setting simply block all the acesss to a URL other than 80

Posting Permissions

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