×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    HTTP connection on 6310i, 7650, 9210 and in sony-ericsson p800

    Hi! I´m having a very serious problem, and I´m quite upset with this problem. I have a problem on opening a http-connection in nokia phones(6310i, 7650, 9210). The phone freezes when the midlet tries to open an http-connection and after a few minutes I get an error "connection not available", or "not responding, try again". On 7650 it opens the gprs-connection after I quit the program. On sony-ericsson this midlet works great! Here is the code of the connection class:


    public class ConnectionImpl extends Connection {
    private String proxyHost;
    private int proxyPort;
    private String sessionKey;
    private HttpConnection connection;
    private InputStream input;
    private OutputStream output;

    private void openHttp(String method, String url) throws IOException {
    closeHttp();

    String host = "" +proxyHost;

    connection = (HttpConnection)Connector.open(host +url, Connector.READ_WRITE, true);
    connection.setRequestMethod(method);
    connection.setRequestProperty("Connection", "close");
    }

    private void closeHttp() throws IOException {
    if (input != null) {
    input.close();
    input = null;
    }

    if (output != null) {
    output.close();
    output = null;
    }

    if (connection != null) {
    connection.close();
    connection = null;
    }
    }

    public ConnectionImpl(String proxyHost, int proxyPort) {
    this.proxyHost = proxyHost;
    this.proxyPort = proxyPort;
    }

    /**
    * Opens a connection with the given host on the given port.
    *
    * @see #close
    */
    public void open(String host, int port, boolean ssl) throws IOException {
    openHttp(HttpConnection.GET, "?command=open&host=" + host + "&port=" + port + "&ssl=" + ssl);
    sessionKey = receive();
    closeHttp();
    }

    /**
    * Closes the connection.
    *
    * @see #open
    */
    public void close() throws IOException {
    try {
    openHttp(HttpConnection.GET, "?command=close&session=" + sessionKey);
    read(new byte[0], 0, 0);
    }
    catch (Exception ignored) {
    }

    sessionKey = null;
    }

    protected int read(byte[] buffer, int offset, int count) throws IOException {
    if (input == null) {
    if (connection == null) {
    write(null, 0, 0);
    }

    if (output != null) {
    output.flush();
    output.close();
    output = null;
    }

    input = connection.openInputStream();

    int response = connection.getResponseCode();
    if (response != HttpConnection.HTTP_OK) {
    String msg = connection.getResponseMessage();
    if (msg == null) {
    msg = "Error " + response + " during HTTP session";
    }

    throw new IOException(msg);
    }
    }

    int actual = input.read(buffer, offset, count);

    if ((actual == -1) && (connection.getLength() != 0)) {
    closeHttp();
    actual = 0;
    }

    return actual;
    }

    protected void write(byte[] buffer, int offset, int count) throws IOException {
    if (output == null) {
    if (input != null) {
    input.close();
    input = null;
    }

    openHttp(HttpConnection.POST, "/" + sessionKey);
    output = connection.openOutputStream();

    //contentLength = 0;
    }

    if (count != 0) {
    output.write(buffer, offset, count);
    //contentLength = contentLength + count;
    }
    }

    public boolean connected() {
    return (sessionKey != null);
    }
    }


    The program is an email-program and I have a servlet running on my computer. I have several days to solve this program, but without success. Is there something wrong in the code, or what?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    5
    Have you tried to put the connection in a new thread ? Look for this tip given in the forum :

    <http://nkn.forum.nokia.com/forum/sh...&threadid=16388>

    Hope it helps ;-)

Posting Permissions

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