File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java Micro Edition and the fly likes How can I recieve data from servlet on j2me application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "How can I recieve data from servlet on j2me application?" Watch "How can I recieve data from servlet on j2me application?" New topic

How can I recieve data from servlet on j2me application?

Nguyen Long Hoang

Joined: Feb 02, 2010
Posts: 1
Hello everybody!
Please! answer this question for me .

I need to recieve data (e.g. a String array) from a servlet which interacts with mysql on j2me application.

Can I do that??

thanks so much.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Assuming you're talking about a CDC device, then the is available, so you can use straight HTTP.

If you're asking about CLDC, then you'll need to fudge something with the classes in the package which -if I interpret the javadocs correctly- implement something akin to sockets.
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
If it's simple string array why don't you just print it to output as a string with delimiters and than parse it on phone.
Anshal Patel
Ranch Hand

Joined: Feb 07, 2010
Posts: 32
sorry for jumping this topic.

I have the same question.

Alexander sir can you please show us a sample code that would be of great help as even I have the same question.

My scenario is retrieving data from sql 2000 database and the printing a particular column for a particular id on mobile

ex select roll_no from student where name = "anshal"

so how do i print roll_no

I tried out.println but that thing prints on the emulator not on screen '

and i am using sun WTK 2.5
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68
(@ Anshal Patel)
I haven't quite understand your problem.
Have you succeed to transfer string to mobile phone or not?

To print on mobile phone you can create textbox and set string as it's value.
out.println can be used for debug purpose but should be removed in production.

You can create something like this:
Create textbox where user will input student name.
After submitting open http connection and send request with name as parameter to webservice (or simple page, whatever)
On server side pick student name, do the sql query on db and send roll_no in response.
On mobile retrieve response and print it into textbox.

Simple example of textbox
Anshal Patel
Ranch Hand

Joined: Feb 07, 2010
Posts: 32
Thanks a ton alexander for the quick reply.

I understood what you said perfectly.

Now I have few questions.

1)In this code tbox = new TextBox("Hello world MIDlet", "Hello World!", 25, 0); can i replace "Hello World " with some string object.

2)Could you please verify whether the code is right for sending data to the servlet.

3)Can you guide me as how to retrieve data from the servlet.

Following is my code

public void connect()

HttpConnection httpconnection = (HttpConnection);
System.out.println("url is" + url);
httpconnection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
httpconnection.setRequestProperty("Content-Language", "en-US");
//byte abyte0[] = connData.getBytes();

// httpconnection.setRequestProperty("Content-Length", ))
OutputStream outputstream = httpconnection.openOutputStream();
byte abyte0[] = connData.getBytes();
for(int i=0;i<abyte0.length;i++)

System.out.println("Connected Successfully");

int i = httpconnection.getResponseCode();
System.out.println("RC" + i);

if(i != 200)
throw new IOException("HTTP response code: " + i);
DataInputStream inputstream = httpconnection.openDataInputStream();
int j = (int)httpconnection.getLength();
System.out.println("Length of inputstream is :"+j);
byte abyte1[] = new byte[j];
int k =;
System.out.println("Length is :"+k);
String abc=httpconnection.getResponseMessage();
String s = new String(abyte1,0,k);
System.out.println("string is:" + s + "******************************"+abc);

Anshal Patel
Ranch Hand

Joined: Feb 07, 2010
Posts: 32
I have picked the paramters using getParameter() and processed the the variables in database and stored them in a string object by


now my question is how do i print the data in string obj 'msg' inside a textbox on mobile emulator
Aleksandar Babic
Ranch Hand

Joined: May 30, 2007
Posts: 68

Yes, you can replace it with String object ( it's string ether way )

If you managed to get response in j2me, than only thing you need to do is to read input stream into String(or StringBuffer) and show it in textbox.
If you don't know how to do that you can do it like this

Haven't tried this one but it might work
Anshal Patel
Ranch Hand

Joined: Feb 07, 2010
Posts: 32
thanks a ton i have created the code and now waiting to get my jdbc problems resolved
Anshal Patel
Ranch Hand

Joined: Feb 07, 2010
Posts: 32
issue resolved thanks aleksander
Archana Maharjan

Joined: Nov 12, 2010
Posts: 5
Hello everyone!
I'm desperately trying to send bytes from servlet to midlet so that i could turn that bytes back to image and display in midlet.
Below are my codes!

//For Servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String MIDP_USER_AGENT = "UNTRUSTED/1.0 Profile/MIDP-1.0 Configuration/CLDC-1.0";
String ua = request.getHeader("User-Agent");
String un=request.getParameter("ACT");
String pwsd=request.getParameter("PWD");
PrintWriter out = response.getWriter();
out.println("screen capture"+request.getParameter("ACT"));
if(un.equals("project") && pwsd.equals("java"))
try {
//ScreenCapture is a class that captures DesktopScreen and returns its bytes
byte[] imgByte=ScreenCapture.ScreenCapture();
} catch (AWTException ex) {
out.println("Sorry!Authentication not valid,Try Again!");

//For Midlet
public void invokeServlet(String geturl, String account, String password) throws IOException {
HttpConnection c = null;
InputStream is = null;
DataInputStream in=null;
StringBuffer b = new StringBuffer();
byte[] imgByts;
try {
c = (HttpConnection) + "?ACT=" + account + "&PWD=" + password);
c.setRequestProperty("IF-Modified-Since", "20 Apr 2002 16:19:14 GMT");
c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
is = c.openDataInputStream();
int ch;
while ((ch = != -1) {
b.append((char) ch);
Image img=Image.createImage(imgByts, 0, imgByts.length);
ControlPCMidlet.canvas = new ImageCanvas(img);
ControlPCMidlet.canvas = new ImageCanvas(image);
} catch (IOException ioe) {
System.out.println("error 2:" + ioe);
} finally {
if (in != null) {
if (is != null) {
if (c != null) {
//Drawing picture in canvas

What should I do in above codes to get screencapture in midlet application? Thanks in advance....
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

Welcome to the JavaRanch, Archana!

You really should have started your own thread. This one has been inactive for months. Plus what' you're asking isn't quite the same thing as what the original discussion was about.

Also, there's a button labelled "Code" on the message editor. You can use this to make your code samples more readable.

An IDE is no substitute for an Intelligent Developer.
Archana Maharjan

Joined: Nov 12, 2010
Posts: 5
Thankyou for the quick reply....Okay I'll start a new thread....
I agree. Here's the link:
subject: How can I recieve data from servlet on j2me application?
It's not a secret anymore!