• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in j2me HTTP connection...ask for airtime charging..:S

 
Issam Tang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ,
I am stuck on this problem for a while I searched the net and couldnt find a solution....I am creating a simple Medlet client and a webserver via Netbeans. anyways the problem is when I try to connect to my server URL which is on the same machine. I get a warning first saying
" Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler."

well like u see my Client Medlet I have the connection in a sperete function (look below)
anyways when I run the client and once it calls :
HttpConnection hc = (HttpConnection)Connector.open(url);

I get a wierd question in the gui interface saying "Client want to send and recieve data using the network. This use you airtime and may result in charges.

is it OK to user airtime? "

so I press yes ofcourse , nothing happens...

I tried even using the http connection example used in netbeans and it gives me the same error...

any help is much appreciated..... ...

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

import org.bouncycastle.crypto.StreamCipher;
import org.bouncycastle.crypto.engines.RC4Engine;
import org.bouncycastle.crypto.params.KeyParameter;
public class StealthMIDlet extends MIDlet {
private Display mDisplay;
private TextBox mTextBox;
private String mSession;
private StreamCipher mOutCipher, mInCipher;
public StealthMIDlet() {
mOutCipher = new RC4Engine();
mInCipher = new RC4Engine();
}
public void startApp() {
if (mSession == null) {
// Load the keys from resource files.
byte[] inKey = getInKey();
byte[] outKey = getOutKey();
// Initialize the ciphers.
mOutCipher.init(true, new KeyParameter(outKey));
mInCipher.init(false, new KeyParameter(inKey));
}
mDisplay = Display.getDisplay(this);
if (mTextBox == null) {
mTextBox = new TextBox("StealthMIDlet", "The eagle has landed", 256, 0);
mTextBox.addCommand(new Command("Exit", Command.EXIT, 0));
mTextBox.addCommand(new Command("Send", Command.SCREEN, 0));
mTextBox.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) notifyDestroyed();
else send();
}
});
}
mDisplay.setCurrent(mTextBox);
}

private void send() {
// Encrypt our message.
byte[] plaintext = mTextBox.getString().getBytes();
byte[] ciphertext = new byte[plaintext.length];
mOutCipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0);
char[] hexCiphertext = HexCodec.bytesToHex(ciphertext);
// Create the GET URL. Our user name and the encrypted, hex
// encoded message are included as parameters. The user name
// and base URL are retrieved as application properties.
//String baseURL = getAppProperty("StealthMIDlet.baseURL");
String baseURL = "http://localhost:8084/Stealth_Server/";
URLBuilder ub = new URLBuilder(baseURL);
ub.addParameter("user", getAppProperty("StealthMIDlet.user"));
ub.addParameter("message", new String(hexCiphertext));
//String url = ub.toString();
String url = "http://www.yahoo.com/";
try {
// Query the server and retrieve the response.
HttpConnection hc = (HttpConnection)Connector.open(url);
if (mSession != null)
hc.setRequestProperty("cookie", mSession);
InputStream in = hc.openInputStream();
String cookie = hc.getHeaderField("Set-cookie");
if (cookie != null) {
int semicolon = cookie.indexOf(';');
mSession = cookie.substring(0, semicolon);
}
int length = (int)hc.getLength();
ciphertext = new byte[length];
in.read(ciphertext);
in.close();
hc.close();
}
catch (IOException ioe) {
Alert a = new Alert("Exception", ioe.toString(), null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mTextBox);
}

// Decrypt the server response.
String hex = new String(ciphertext);
byte[] dehexed = HexCodec.hexToBytes(hex.toCharArray());
byte[] deciphered = new byte[dehexed.length];
mInCipher.processBytes(dehexed, 0, dehexed.length, deciphered, 0);
String decipheredString = new String(deciphered);
Alert a = new Alert("Response", decipheredString, null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mTextBox);
}
// Normally you would probably read keys from resource files
// in the MIDlet suite JAR, using the getResourceAsStream()
// method in Class. Here I just use hardcoded values that match
// the hardcoded values in StealthServlet.
private byte[] getInKey() {
return "Incoming MIDlet key".getBytes();
}
private byte[] getOutKey() {
return "Outgoing MIDlet key".getBytes();
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Issamo"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First the alert warning the user is always going to appear, unless the Permission is listed in the JAD file.

When the warning is issued the user has options of denying, allowing just this once, or always allow.

What exception or error are you actually getting after you select Yes to the warning?

Mark

p.s. we have buttons under the Add Reply button. One of them is for posting code to keep the formatting like indentation. Otherwise it is next to impossible to read.
 
Issam Tang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks man for ur reply.....well yah this warning appears all the time.
I dont have any error message...but simply after I press Yes or No, nothing happens, I dont get any messages back or even an error. I have my server running at the same machine and no requests recived from the client. so basically after pressing yes the client doesnt send anything. and that aittime charging message doesnt go away.

really stuck on this thing for more than a week...

I hope somebody faced this so he/she can help...
 
Glenn Castro
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your url. Your url must be the location where your server application is running.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in the JAD file there is a midlet-permissions "tag" that you can use to include the permission for httpConnection. I don't remember the exact permission class name to post that here.

Yes, check the URL that it is correct. I assume you are using the Wireless Toolkit, and the Web Server is on the same machine, you can use localhost at the url to just do a check. But again my memory fails to know which port or such to use localhost.

I am having a bad memory morning. Please excuse me.

Mark
 
Issam Tang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok guys I solved the problem by creating the connection mechanism in a sperate thread...but now my server is not getting any requests from the client
I have both client running as part of a mobile aplication in Netbeans..and the server created as a tom cat web applications using netbeans too....I run the server first, take its address which is in my case
String url = "http://localhost:8084/Stealth_Server/";
HttpConnection hc = (HttpConnection)Connector.open(url);

so no communication happening....any hints about my problem ? ..

thanks..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic