This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

send audio and text via one http connection?

 
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!
 
author
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!
 
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
Boost this thread!