Midlet displaying data from a servlet

Login to reply to this topic.
Thu, 2005-09-22 22:17
Joined: 2005-09-21
Forum posts: 6

Hi
I am writing an application for a mobile. My midlet connects to a java servlet which has a database connection. So far information received from the database, is displayed as follows
On the servlet:
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
Â…Â…Â…..
Â…Â…Â…..
rs = stmt.executeQuery("SELECT * FROM food");
while(rs.next())
{
    out.print(rs.getObject(1).toString());
    out.print(rs.getObject(2).toString());
    out.print(rs.getObject(3).toString());
   
}

And in the midlet, the information is got by
textFromServer  = new String(servletData);
append(textFromServer);

The above snippets of code works and displays all the data from the database.
Problem is it just displays one long string.
How do I separate the “servletData” out so that I can add a choice group to each row?

Do I need to use xml to help me in design? I donÂ’t know how to use xml or where to start with it. Any pointers?

Thank you

Fri, 2005-09-23 05:59
Joined: 2005-08-10
Forum posts: 14
Re: Midlet displaying data from a servlet
Hi lifeline,

 I guess first of all u use a seperator like \n  between the rows when u send the data back to the
 mobile and then in the midlet, use this seperator to segregate every single row and collect
 these in a String[].
 After that u can add these strings into the choicegroup.
 I hope this should solve ur problem.


 Cheers,
 Amulya

ARB

Sat, 2005-09-24 17:58
Joined: 2005-09-21
Forum posts: 6
Re: Midlet displaying data from a servlet
Thank you for your suggestion Amulya.
I was trying to do as you said using split()

// Getting string from server
 User_Name = new String(servletData);

//each word is divided by a *
//store each word in an array
String[] starwords = User_Name.split("\\*");
for (int i=0; i<starwords.length; i++)
  {append(starwords[i]);}

This worked great until I added the code to my midlet.
spilt() is not recognised by midp.
Any other ideas of how I could break up the string from the servlet?

Thank you
Mon, 2005-09-26 09:04
Joined: 2005-08-10
Forum posts: 14
Re: Midlet displaying data from a servlet
Hi lifeline,

   u r right. split() function is not supported by midp.
   but still i think we can fashion something.
   use
    indexOf(String ) to find the first instance of the seperator, and use the subString method to get  the string uptill that.
   next use
   indexOf(String , int fromindex) to find further instances of the seperator, and use the subString
method to get the required strings.
  the above methods have to be kept in the loop.
  I think this should work since all the above methods are supported by midp.
  Tell me if even this doesn't work.

Cheers,
Amulya

ARB

  • Login to reply to this topic.