• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I recieve data from servlet on j2me application?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you're talking about a CDC device, then the java.net.URLConnection 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 javax.microedition.io package which -if I interpret the javadocs correctly- implement something akin to sockets.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(@ 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
http://www.devx.com/tips/Tip/20098
 
Anshal Patel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()

{
try
{
HttpConnection httpconnection = (HttpConnection)Connector.open(url);
System.out.println("url is" + url);
httpconnection.setRequestMethod(httpconnection.POST);
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++)
{
outputstream.write(abyte0[i]);
}

outputstream.flush();
outputstream.close();
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 = inputstream.read(abyte1);
System.out.println("Length inputstream.read is :"+k);
String abc=httpconnection.getResponseMessage();
String s = new String(abyte1,0,k);
System.out.println("string is:" + s + "******************************"+abc);



}
 
Anshal Patel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have picked the paramters using getParameter() and processed the the variables in database and stored them in a string object by

msg=res.getString(1);

now my question is how do i print the data in string obj 'msg' inside a textbox on mobile emulator
 
Aleksandar Babic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a ton i have created the code and now waiting to get my jdbc problems resolved
 
Anshal Patel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
issue resolved thanks aleksander
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
if(MIDP_USER_AGENT.equals(ua)){//Midlet
out.println("screen capture"+request.getParameter("ACT"));
out.println("password:"+request.getParameter("PWD"));
}
else{
response.setContentType("text/plain");
if(un.equals("project") && pwsd.equals("java"))
{
try {
//ScreenCapture is a class that captures DesktopScreen and returns its bytes
byte[] imgByte=ScreenCapture.ScreenCapture();
out.print(imgByte);
} catch (AWTException ex) {
ex.printStackTrace();
}
}
else{
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) Connector.open(geturl + "?ACT=" + account + "&PWD=" + password);
c.setRequestMethod(HttpConnection.GET);
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 = is.read()) != -1) {
b.append((char) ch);
}
imgByts=b.toString().getBytes();
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) {
in.close();
}
if (is != null) {
is.close();
}
if (c != null) {
c.close();
}
}
//Drawing picture in canvas
ControlPCMidlet.display.setCurrent(ControlPCMidlet.canvas);
}

What should I do in above codes to get screencapture in midlet application? Thanks in advance....
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Archana Maharjan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for the quick reply....Okay I'll start a new thread....
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic