Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

send audio and text via one http connection?

 
Pam Ludford
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to send an audio file (converted to a byte array) and text data in one http output stream from a J2ME cell phone client that is MIDP 1.0.

What is the easiest way to do this and still be able to differentiate the audio byte array from the text data on the servlet side?

I have done alot of googling on this and don't seem to find a straightforward solution:

-MIME content type "multipart/mixed" seems plausible, but I only see it used in email clients, and I'm not sending an email message

- I see Michael Yuan says you can send an image file as a byte array over http using DataOutputStream, but how would you send text data, too? Should you set up your own token system? Perhaps you could insert the byte array length into the DataOutputStream, read it on the servlet side starting at the offset for length number of bytes? And then assume everything after that is string data?

...or is there another way? I assume this is the same as sending an image as a byte array...

Thanks!
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I do:



On the other end, you can reconstruct the data.

 
Pam Ludford
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million, I'll give that a try!
 
thanos ton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I want to do something similar (Send a file in bytes).
I am doing like that:

J2ME

String requestString = "POST message";

hc = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );
hc.setRequestMethod( HttpConnection.POST );

dos = hc.openDataOutputStream();

byte[] requestBody = requestString.getBytes();
dos.writeInt (requestBody.length);
dos.write(requestBody, 0, requestBody.length);
dos.writeUTF ("utf string");

Servlet's doPost

is = request.getInputStream();
dis = new DataInputStream(is);

byte [] audiodata = null;
int length = dis.readInt();
dis.readFully(audiodata, 0, length);
String text = dis.readUTF ();

Then I send a response message back to the midlet

I receive a null pointer exception in

dis.readFully(audiodata, 0, length);

One time only (dont know why) I received an end of file exception in the same line!

the int and udf write/reading works ok (they are there for testing)

any ideas please?
 
thanos ton
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doh!
ok sorry,
the byte array was obviously null...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic