×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    problem in j2me program on Nokia 6630/6680

    Hi,

    I wrote a j2me program that reads from a file using the FileConnection package (JSR 75), and sends this content to the server using HTTP. I have used threads to automate this program. The program reads the contents of file first time and sends them to server and then sleeps for 60 secs, then again after 60 secs, it reads the contents again and if data is stale it sleeps again for 60 secs, or if file data is new, it sends this data to server, this keeps happening until the EXIT button is pressed. So basically, my program reads the file after every 60 secs. Every time i read, i close the file too. This program is working properly on the emulator, but when i transferred it to Nokia 6630, it reads the contents of the file the first time properly and then next time it asks whether the J2ME application is allowed to read/write user data and then nothing happens. Nothing is displayed when program tries reading the file the 2nd time. After that the program gets stuck. But am able to exit it.
    I tried replacing the file reading part in the same program with displaying a simple string, and its working properly on the phone. The string is getting displayed after every 60 secs. So there is no problem in thread handling. But file reading is happening only once. Do you have any idea why this may be happening? I have attached my code, can u pls check it and let me know if thr's something wrong. It would be really helpful, am really stuck. Here's my j2me code:


    // project name is ModularCleanThreadFileMidlet

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


    /*
    An example MIDlet which reads a file and sends its contents to a servlet.
    */

    public class ModularCleanThreadFileMidlet extends MIDlet implements Runnable, CommandListener
    {
    private Display display;
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private Form mainForm;
    String url = "http://10.129.139.164:9000/UpdateServlet";

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

    //Initialization. Invoked when MIDlet activates
    public void startApp()
    {
    mainForm = new Form("Invoking servlet");
    mainForm.addCommand(exitCommand);
    mainForm.setCommandListener(this);
    display.setCurrent(mainForm);
    new Thread(this).start();
    }

    public void run()
    {
    try
    {
    String str="";
    while(true)
    {
    System.out.println("reading file");
    String hold = readFile("gpsdata.txt");
    if((hold.compareTo(str))==0)
    {
    // make the thread sleep
    mainForm.append("Stale data: " + hold);
    Thread.sleep(15000);
    }
    else
    {
    str = hold.toString();
    //invokeServlet(url,hold);
    mainForm.append("New data: " + str);
    Thread.sleep(15000);
    }
    }
    } catch (Exception e)
    {
    System.out.println("Exception " + e);
    e.printStackTrace();
    }
    }

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

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

    String readFile(String fileName) throws IOException, IllegalArgumentException, InterruptedException
    {
    TextBox t = null;
    FileConnection fc = (FileConnection)Connector.open("file:///E:/Others/" + fileName);
    if(!fc.exists())
    {
    mainForm.append("MIDlet says: Required file not found!");
    //t = new TextBox("MIDlet says..", "Required file not found!", 1024, 0);
    display.setCurrent(mainForm);
    throw new IOException("File does not exist");
    }
    InputStream in = fc.openInputStream();
    byte temp[] = new byte[1024];
    int length = in.read(temp, 0, 1024);
    String content = new String(temp,0,length);
    fc.close();
    return content;
    }

    //Prepare connection and streams then invoke servlet.
    void invokeServlet(String url, String hold) throws IOException, IllegalArgumentException, InterruptedException
    {

    HttpConnection c = null;
    InputStream is = null;
    OutputStream dos = null;
    StringBuffer b = new StringBuffer();
    TextBox t = null;
    try{

    // http code starts, contents if file are in string variable 'hold'
    c = (HttpConnection)Connector.open(url);
    c.setRequestMethod(HttpConnection.POST);
    dos = c.openOutputStream();
    String output= "9789,";
    output=output.concat(hold);
    System.out.println("output: " + output);
    byte postmsg[] = output.getBytes();
    for(int i=0;i<postmsg.length;i++)
    {
    dos.write(postmsg[i]); // Sending gpsdata
    }
    //dos.flush();
    is = c.openDataInputStream();
    int ch;

    // receive response and display it in a textbox.
    while ((ch = is.read()) != -1)
    {
    b.append((char) ch);
    }

    //mainForm.delete(0);
    mainForm.append(b.toString());

    }catch(Exception ce)
    {

    mainForm.append("MIDlet says: Connection with Server could not be established!");
    //mainForm.append(ce.toString());
    display.setCurrent(mainForm);
    }


    finally {
    if(is!= null) {
    is.close();
    }
    if(dos != null) {
    dos.close();
    }
    if(c != null) {
    c.close();
    }
    }
    display.setCurrent(mainForm);
    //display.setCurrent(t);
    }

    public void commandAction(Command c, Displayable s)
    {
    if (c == exitCommand)
    {
    destroyApp(false);
    notifyDestroyed();
    }
    }
    }// end class

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Re: problem in j2me program on Nokia 6630/6680

    Hi,

    I’m a newcomer to the “Java universe” and especially to Java ME and Nokia Wireless products and services. However I try to give you some idea based on my general understanding of Java technology.

    a) First of all try to check the configuration of installed (system) software on 6630.

    b) Take a closer look at Javadocs of APIs, located in Java ME java/io-package

    c) Take a closer look at Java ME security mechanisms. Maybe you find here some useful info or idea… (I.e. record store API doesn’t know where the MIDlet record stores are located!)

    d) File system access of Java ME applications are controlled by native OS, and defined by associated J2ME API (I think JSR 75).
    JSR can be implemented partly or completely by device vendors. (That is why a MIDlet on different device behaviours differently.)
    Here you may find an answer and maybe a solution too.

    However, I think the “confirmation.request” (for re-opening the file) comes from underlying OS and if no any mechanism is provided to disable it programmatically (by use of related implemented Java ME APIs), and then I don’t see any chance more. So you may think about a new approach (in communication mit Nokia and Sun Microsystems software support)

    I’m interested to know which SDK of Nokia Series60 SDKs are you using and within which IDE?

    Bye,
    AskiToday

Similar Threads

  1. Nokia N-Series Complete Model Line-Up
    By Dopod in forum General Development Questions
    Replies: 6
    Last Post: 2008-07-22, 20:16
  2. Replies: 2
    Last Post: 2008-04-03, 19:02
  3. Java Compability
    By emax73 in forum General Development Questions
    Replies: 0
    Last Post: 2004-07-12, 14:56
  4. Nokia Developers Suite for J2ME problem
    By CptBalu in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2004-03-15, 17:09

Posting Permissions

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