• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

network midlet problem

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made several code that connect to the network, but when i run the application i always
get message like this-->
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.
Do i have to change the properties in order to get the networking done?


cheers
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pingkan paula,
The related class should implement the Runnable interface.

public class YourClass extends ... implements Runnable{

...

public void commandAction(Command c, Displayable d) {
if( c == resume ){
Thread t = new Thread(this);
t.start();
else if(...){
....
}
}

----
public void run() {
try {
connect();
}
catch (..) {
...
}
}

}

[ February 11, 2005: Message edited by: Burak Celebi ]
[ February 11, 2005: Message edited by: Burak Celebi ]
 
pingkan paula
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still can't get the network , when i enable network monitoring from emulator preferences there was nothing happened. Here is the code:
 
Burak Celebi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again pingkan paula,

Here is the thread safe version of corej2me's pngviewer:



/*--------------------------------------------------
| PngViewer.java
|
| Download and view a png file
|
| Web: www.corej2me.com
|
| Version: 1.0 - Original Release
| Version: 1.1 - Updated getImage() to use
| readFully() and also support reading
| one character at a time when length
| is not available
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

/*--------------------------------------------------
* Main class definition
*-------------------------------------------------*/
public class PngViewer extends MIDlet implements CommandListener, Runnable {
private Display display;
private TextBox tbMain;
private Form fmViewPng;
private Command cmExit;
private Command cmView;
private Command cmdBack;

/*--------------------------------------------------
* Constructor for the class.
*-------------------------------------------------*/
public PngViewer() {
display = Display.getDisplay(this);

// Create the Main textbox with a maximum of 50 characters
tbMain = new TextBox("Enter png url", "http://www.corej2me.com/scratch/spin.png", 50, 0);

// Create commands and add to textbox
cmView = new Command("View", Command.SCREEN, 1);
cmExit = new Command("Exit", Command.SCREEN, 1);
tbMain.addCommand(cmView );
tbMain.addCommand(cmExit);

// Set up a listener to "capture" button presses
tbMain.setCommandListener(this);

// ---------------------------------------

// Create the form that will hold the png image
fmViewPng = new Form("Png Image");

// Create commands and add to form
cmdBack = new Command("Back", Command.BACK, 1);
fmViewPng.addCommand(cmdBack);

// Set up a listener to "capture" button presses
fmViewPng.setCommandListener(this);
}
// -----------------------------------------------------------------------------------------------
/*--------------------------------------------------
* Called by the Application Manager on the device
* to start the MIDlet.
*-------------------------------------------------*/
public void startApp() {
// Display the Main textbox
display.setCurrent(tbMain);
}
// -----------------------------------------------------------------------------------------------
public void pauseApp() {
}

// -----------------------------------------------------------------------------------------------

public void destroyApp(boolean unconditional) {
}
// -----------------------------------------------------------------------------------------------
/*--------------------------------------------------
* Process events
*-------------------------------------------------*/
public void commandAction(Command c, Displayable s) {
// If the Command button pressed was "Exit"
if (c == cmExit) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmView) // If the Command button pressed was "View"
{
// Remove anything that may be on the form
if (fmViewPng.size() > 0)
for (int i = 0; i < fmViewPng.size(); i++)
fmViewPng.delete(i);

Thread t = new Thread(this);
t.start();

} else if (c == cmdBack) // If the Command button pressed was "Back"
display.setCurrent(tbMain);
}
// -----------------------------------------------------------------------------------------------
public void run(){

try {
// Get the image from the web and append to the form
Image im;
if ((im = getImage(tbMain.getString())) != null)
fmViewPng.append(im);

// Display the form with the image
display.setCurrent(fmViewPng);
} catch (Exception e) {
System.err.println("Msg: " + e.toString());
}
}

// -----------------------------------------------------------------------------------------------

/*--------------------------------------------------
* Open an http connection and download a png file
* into a byte array.
*-------------------------------------------------*/
private Image getImage(String url) throws IOException {
ContentConnection connection = (ContentConnection) Connector.open(url);
DataInputStream iStrm = connection.openDataInputStream();

Image im = null;

try {
// ContentConnection includes a length method
byte imageData[];
int length = (int) connection.getLength();
if (length != -1) {
imageData = new byte[length];

// Read the png into an array
iStrm.readFully(imageData);
} else // Length not available...
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();

int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);

imageData = bStrm.toByteArray();
bStrm.close();
}

// Create the image from the byte array
im = Image.createImage(imageData, 0, imageData.length);
} finally {
// Clean up
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
}
return (im == null ? null : im);
}
// -----------------------------------------------------------------------------------------------
}

cheers!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic