sending from MIDlet, receiving in PHP on server

View: New views
3 Messages — Rating Filter:   Alert me  

sending from MIDlet, receiving in PHP on server

by johnyjj2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello :-)!

Summary: I'd like to send text file from MIDlet on mobile phone to PHP on server. There's problem with method encode(java.lang.String) in MIDlet. I can post code of MIDlet, if needed.

------- MIDlet ----------

I want to create MIDlet, which can send with the use of POST method and httpconnect little text file (or just string variable) to the server. The server would receive this file (or string) with the use of php.

Unfortunately, MIDlet doesn't compile in Wireless Toolkit 2.5.2_01 for CLDC. There is error about encode function, which changes string variable to the format allowed by httpconnection:

[quote]C:\Documents and Settings\MainAccount\j2mewtk\2.5.2\apps\myMidletSendData\src\myMidletSendData.java:90: cannot find symbol
symbol : method encode(java.lang.String)
location: class myMidletSendData
String encodedData = encode( rawData ); // user-supplied
                     ^[/quote]

I guess it may be problem with includes. This is what I've got:
[code]import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;[/code]

-------- PHP ------

The other question is about PHP. I've got book about PHP and example how to create file php which contains form responsible for sending file from computer in web navigator to server. I also have got code responsible for receiving this file (from directory where there are stored temporarily stored files received by the server).

In my case it is not web browser (like IE or MF) which sends the file, but MIlet. I guess it is not big difference because in both cases files received by server are stored in temporary folder. However I don't know how to know name of variable connected with the received file. In the case of sending from web browser to server this name is used both in php responsible for sending and receiving:
File in web browser: [code]Load this file:<input name="usersfile" type="file">[/code]
File to receive: [code]&usersfile = $HTTP_POST_FILES['usersfile']['tmp_name'];[/code]
In MIDlet: [code]HttpConnection conn = null;
                String url = "http://87.205.xx.xx/datareceiver.php";[/code]
In the case of MIDlet sending it to server, I don't know how php on server should know name of this file.

Greetings :-)!

Re: sending from MIDlet, receiving in PHP on server

by josergc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi johnyjj2,

Can I ask you in what class is defined the function "encode"? if it is defined by you, can you post the code?

About PHP, if you are going to send the data as a file, remember to add the "multipart/form-data" in the HTTP headers: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
Other way is if you send the data as the same way that with GET, that is att1=value1&att2=value2&...&attN=valueN, the PHP already parses it and your value will located at the array $_POST.

Re: sending from MIDlet, receiving in PHP on server

by johnyjj2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for your answer :-)

About "encode":
In fact I thought this function should be defined in one of the classes which were included in the example. I made my code with the use of three source codes which I found in the internet:
1) http://developers.sun.com/mobility/midp/ttips/HTTPPost/ <- especially this one
2) http://www.java2s.com/Code/Java/J2ME/SampletodemonstrateHttpGETandPOSTfromMIDlet.htm
3) http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139

About "multipart/form-data":
Now I understand the difference between "application/x-www-form-urlencoded" and "multipart/form-data" :-), thanks. It looks like in simple version I can use the default "application/x-www-form-urlencoded" and send string.
I guess what I have written previously should be OK: [code]sendTextFile("123456789012 234567890123 end");[/code] if there would be proper encoding, changing it to value=123456789012+234567890123+end. But what should I include to support proper encoding for x-www-form-urlencoded? I guess there is no need for me to implement this encode function because it should already be implemented somewhere.

And about array $_POST:
The crucial thing is that I don't have much experience with PHP :-(. I know there exists this table with all the values received by PHP on server. I guess there should be some code in PHP run on server but I don't know how to have this code to listen all the time for receiving POST messages and saving it to database or to text files in one given directory. I guess I should soon learn how to do it (I hope I would :-)).
On the other hand I've got terrible configuration of server (I'm not admin of this server) - there are already installed PHP, Apache, IIS, KeyFocus Web Server - some of those things do the same but I guess their ports are somehow redirected on the router. At this moment I need to decide - what to choose to receive data on server. At first I thought about PHP. (But: look at my new approach below). In order to do it, I guess I need to read the whole install.txt of PHP because it doesn't work properly.

About new approach:
http://codetrips.blogspot.com/2007/04/http-post-from-j2me-midlet.html <- If I've got Apache on my server (and other things mentioned above) - would it be good idea to install this Apache Tomcat JSP container?

Greetings :-)!