×

Discussion Board

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

    Read a content from a file with HTTPConnection on Nokia 7650

    I've done a midlets that read a content from a file through HttpConnection. In the emulator these work fine, but on Nokia 7650 they don't.

    Example of my Midlet:

    package com.poqit.j2me.client;

    import java.io.*;

    import javax.microedition.midlet.MIDlet;
    import javax.microedition.lcdui.TextBox;
    import javax.microedition.lcdui.Command;
    import javax.microedition.lcdui.CommandListener;
    import javax.microedition.lcdui.Display;
    import javax.microedition.lcdui.Displayable;
    import javax.microedition.lcdui.TextField;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.io.Connector;

    public class FileRead extends MIDlet implements CommandListener {
    private final static String URL =
    "http://albatros.fri.uni-lj.si:8080/Rok_Mitja/J2ME/contacts.csv";
    private static final int TEXTBOX_LENGTH = 256;
    private Command exitCommand;
    private Command okCommand;
    private Display display;
    private String file = "";

    public FileRead() {
    display = Display.getDisplay(this);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    okCommand = new Command("OK", Command.OK, 2);
    }

    public void startApp() {
    TextBox t = new TextBox("Read file:", URL, TEXTBOX_LENGTH,
    TextField.ANY);
    t.addCommand(exitCommand);
    t.addCommand(okCommand);
    t.setCommandListener(this);
    display.setCurrent(t);
    }

    public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT) {
    notifyDestroyed();
    return;
    }

    if (c.getCommandType() == Command.OK) {
    TextBox t = (TextBox) s;
    String result = read(t.getString());
    if (result.length() > TEXTBOX_LENGTH) {
    result = result.substring(0, TEXTBOX_LENGTH - 1);
    }
    t.setString(result);
    t.removeCommand(okCommand);
    }
    }

    public final static String read(String url) {
    String result = null;
    HttpConnection hc = null;
    InputStream is = null;
    try {
    hc = (HttpConnection) Connector.open(url);

    hc.setRequestMethod(HttpConnection.GET);
    String profiles = System.getProperty("microedition.profiles"
    String configuration =
    System.getProperty("microedition.configuration"
    hc.setRequestProperty("User-Agent",
    "Profile/" + profiles + " Configuration/"
    + configuration);
    String locale = System.getProperty("microedition.locale"
    if (locale != null) {
    hc.setRequestProperty("Content-Language", locale);
    }
    String platform = System.getProperty("microedition.platform"
    if (platform != null) {
    hc.setRequestProperty("J2ME-Platform", platform);
    }

    try {
    if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
    is = hc.openInputStream();

    InputStreamReader isReader = new InputStreamReader(is);
    int len = (int) hc.getLength();
    if (len > 0) {
    char[] chars = new char[len];
    int actual = isReader.read(chars);
    result = new String(chars);
    } else {
    StringBuffer sb = new StringBuffer();
    int ch;
    while ((ch = is.read()) != -1) {
    sb.append((char) ch);
    }
    result = sb.toString();
    }
    } else {
    result = "Error: return code was " + hc.getResponseCode();
    }
    } catch (IOException e) {
    result = "Error: is the web server running?";
    }
    finally {
    if (is != null) {
    is.close();
    }
    if (hc != null) {
    hc.close();
    }
    }
    } catch (IOException ioe) {
    result = "Error: IO exception.";
    }
    return result;
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {}

    public void exit() {
    destroyApp(true);
    notifyDestroyed();
    }
    }

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Tampere / Finland
    Posts
    201

    RE: Read a content from a file with HTTPConnection on Nokia 7650

    Hello,

    Could you explain more specifically how the phone behaves when starting the HTTP connection? what kind of error messages it throw? What's the firmware version the phone has?

    I tested your code in Nokia 7650 (sw 4.39) it works very well. sw 3.12 and sw 3.16 have some problems with HttpConnection: the http request should be started in a new thread, otherwise the phone hangs.

    Regards,
    Kui

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

    RE: Read a content from a file with HTTPConnection on Nokia 7650

    Hello

    I have upgraded the software on my nokia 7650 and now the midlet work perfect. Thank you for your help and time !

Posting Permissions

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