Skip to main content

WHY my j2me app stops sending http data...

10 replies [Last post]
mcapelati
Offline
Joined: 2009-08-07

Hi folks,

Hi have an J2ME application that should get some data and send them to an external php application.

Everything was working fine. But now, misteriously, it just stops to send data (!).

There was a change on the server - but I've never heard that there was some incompatibility with j2me and linux+apache+php...

Another thing was I upgrade to Netbeans 6.5.1.

I've lost 2 whole days looking for the problem, and nothing...

Watching by Netbeans debugger, all data are fine, and variables are with right values - but it simply does not POST the data to php (on php app, seems that nothing was sent, but on Apache LOG, there were a call...).

Could by any bad configuration on Netbeans Project? I tried to change many different things, but nothing works!

Following is part of the code - maybe somebody could see a thing I could not (please, help me!!!):

1. public class HttpHandler extends Thread {
2.
3.
4. String url="http://www.servidor.com.br/sistema/java/j2me_rec.php";
5. protected StringBuffer buff=new StringBuffer();
6. String rep=null;
7. public String sendData=null;
8. HttpListener hListener = null;
9.
10. public HttpHandler(HttpListener h){
11. hListener = h;
12. }
13.
14. synchronized public void run(){
15. try{
16. HttpConnection hc = (HttpConnection)Connector.open(url);
17. hc.setRequestMethod(HttpConnection.POST);
18. hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
19. //hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
20. hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
21. DataOutputStream dos=hc.openDataOutputStream();
22. dos.write(sendData.getBytes());
23. dos.flush();
24. dos.close();
25.
26. if (hc.getResponseCode()==hc.HTTP_OK) {
27. // You have successfully connected.
28. DataInputStream dis = hc.openDataInputStream();
29. // Now Process your request.
30. int ch = dis.read();
31. while (ch!=-1){
32. buff.append((char)ch);
33. ch = dis.read();
34. }
35. rep=buff.toString();
36. hListener.processRequest(rep);
37. dis.close();
38. }
39. }catch(SecurityException se){
40. se.printStackTrace();
41. }
42. catch(IOException ioe){
43. System.out.println(ioe.getMessage());
44. }
45. }
46. }

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15

Don't close your output stream. Save that till after you have your input then close both.

Also don't write out all of your data in one call unless you know that it will never be more than a few hundred bytes. If you start trying to send binary images or video files you need to break up your data writes to the server into smaller 512, 1k or 2k chunks so the carrier doesn't drop the connection.

-Shawn

mcapelati
Offline
Joined: 2009-08-07

I did not close the output stream, and problem still occurs...

I wondering if I am sending data in right way. See, the sendData has the value like:

"login=01180809090&password=909090&app_version=1.0&ip_orig=&language=ptBR"

It is right sending that way?

Note:These values were get on form.

sfitzjava
Offline
Joined: 2003-06-15

If the server page is designed to GET the information, and not have it POSTed to it then you should put your information on the URL.

Your ip_orig does not have a value which may be causing some type of server parsing issue.

You might try putting a '?' at the start of your data string so it would begin like "?login=0..".

-Shawn

mcapelati
Offline
Joined: 2009-08-07

Thanks Shawn,

I 've try to use GET and POST, without success.

I even have put "?" on data string (at its beggining), and put a value on ip_orig variable, and nothing...

PHP program is trying to get data by $_REQUEST, and no one value could be read...

Do you think that could be any server problem?

Is there any way to see, on the server, what information has been received?

sfitzjava
Offline
Joined: 2003-06-15

Try accessing the URL you have listed from a desktop browser. When I do I'm getting a file not found. Looks like your server is not set up correctly.

-Shawn

mcapelati
Offline
Joined: 2009-08-07

Oh, sorry about that Shawn - for secutiry purposes, that URL was just an example... But the correct URL is beeing accessed with no problems.

I even have an HTML page for tests, and it could call the PHP and pass the parameters correcty.

If you wanna see it, please, PM me. I could even send you the project: if you could help me, I will be very grateful !

sfitzjava
Offline
Joined: 2003-06-15

One last thought, try commenting out both line 23 & 24:
23. dos.flush();
24. dos.close();

Maybe your phone/provider is now sending the data in chunked format and the server may not like that, or the carriers gateway may not like it.

If that doesn't solve it I'm all out of thoughts.

Good luck
-Shawn

mcapelati
Offline
Joined: 2009-08-07

Thank you so very much, Shawn!!!

You just save my day. I love you !

Hey... just kidding... ;-p

xylifyx
Offline
Joined: 2003-06-22

Some HttpConnection implementations doesn't handle

HTTP/1.1 301 Moved Permanently

or similar non ok return codes, could be something like that. Is the response code HTTP_OK?

mcapelati
Offline
Joined: 2009-08-07

Yes, the response code is HTTP_OK: the code is passing through the IF.

But, the code is not passing by next WHILE, cause ch has the value -1

if (hc.getResponseCode()==hc.HTTP_OK) {
// You have successfully connected.
DataInputStream dis = hc.openDataInputStream();
// Now Process your request.
int ch = dis.read();
while (ch!=-1){
buff.append((char)ch);
ch = dis.read();
}

I mean, the code
int ch = dis.read();
is returning -1