You Are Here:

Community: Developer Discussion Boards

#1 Old problem in a j2me program on Nokia 6630/6680 - 2006-06-28, 16:12

Join Date: Mar 2006
Posts: 4
lappard
Offline
Registered User
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
Reply With Quote

#2 Old Re: problem in a j2me program on Nokia 6630/6680 - 2006-06-29, 02:43

Join Date: Dec 2005
Posts: 1,696
Location: Europe/Poland/Warsaw
peterblazejewicz
Offline
Super Contributor
hi lappard ,

I think that's still a bug with Choise.POPUP on some devices,
I've rewrote your code and tested on 6680 and it does not work (works fine in s60 3rd ED SDK Emulator)
here is related thread:
http://discussion.forum.nokia.com/fo...ad.php?t=54586
http://discussion.forum.nokia.com/fo...ad.php?t=66460
http://discussion.forum.nokia.com/fo...ad.php?t=66311
try different solution, for example switch to screen with selection, etc,

regards,
Peter
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
SERIOUS PROBLEM IN NOKIA 6230i Cybertraker General Discussion 0 2005-06-16 10:17
Nokia Developers Suite for J2ME problem CptBalu Mobile Java Tools & SDKs 0 2004-03-15 18:09
Series 60Series 60 MIDP Concept SDK Beta 0.2 Linux bug? kauppi Mobile Java Tools & SDKs 3 2003-04-07 10:05

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia