This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to transfer data

 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know in general XML is the best way to transfer data. Because in a Servlet what gets returned is text. However in some cases the extra code for reading the XML is unneccessary.
For instance, I have a login screen, and I want to return the user ID and also a list of jobs this user has access to. Usually it won't be more than 5 jobs for a user. So returning XML might be overkill. I was thinking of a straight text with a comma seperator as what should be returned. as in, "727, 12345, 65345, 12124, 62166" where 727 is the user ID and the other 4 are job numbers.
But if I recall the StringTokenizer is not a class in the J2ME jar file.
What do you guys think?
Thanks
Mark
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case, the easiest way to go is to use
DataOutputStream.writeInt()
on the server side to write the five integers to the HTTP stream. On the client side, you can retrieve them in order by calling
DataInputStream.readInt()
five times.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So then the first int would have to be how many numbers are coming back, since it is not always just 5 times.
Thanks Michael
Mark
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael's right. Sometimes it's useful though to have a human-readable data format (e.g. makes testing easier in some cases). In that case, you can just use comma-separated data as you originally described. Although StringTokenizer isn't available, it's little extra work to use String's indexOf(',') and substring methods. You'd probably also take advantage of String's trim() method to get rid of extra whitespace.
 
Niklas Junel
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used a comma-separated txt-file for my j2me, when I read it in the midlet I read one character at a time, in that way I could check for a comma, if I found one, I knew the next "job" was up!
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Niklas, but this thread is almost two years old, and two jobs ago.

Mark
 
Mateen Dar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i faced the same problem . their is no split function in j2me. so i just wrote my own function thata takes a string and a delimiter. about 5 lines of code. thats all.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, if there are any statements or questions, please create a new thread.

Don't resurrect this two year old thread, Or I will delete it.

Not because I am a Sheriff, but because I started the thread in the first place back then.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic