How to send data to a Database from a J2ME Phone01/04/06 PermalinkFirstly send the data over HttpConnection:
String post(String url){
String posted;
HttpConnection connection= null;
try
{
connection=
(HttpConnection)Connector.open(
url,
Connector.READ_WRITE,
true);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("User-Agent","Mobile-Utopia.com Agent" );
DataOutputStream out= connection.openDataOutputStream();
out.writeUTF(content);
InputStream in= connection.openInputStream();
byte[] buffer= new byte[100];
StringBuffer sb= new StringBuffer(100);
sb.append("Server message:\n");
int read;
while (((read= in.read(buffer)) != -1))
sb.append(new String(buffer, 0, read));
out.close();
in.close();
posted=sb.toString();
}
catch (Exception e)
{
posted= "Posting problem: " + e.toString()+";
}
return posted;
}
Then collect the data (using a servlet) at the server end:
DataInputStream dataIn= new DataInputStream(request.getInputStream());
String all= dataIn.readUTF();
// ..post all to database
|
|