How can I recieve data from servlet on j2me application?
Nguyen Long Hoang
Greenhorn
Joined: Feb 02, 2010
Posts: 1
posted
0
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.
This message was edited 1 time. Last update was at by Bear Bibeault
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32421
posted
0
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.
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
posted
0
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
posted
0
(@ 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.
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
Joined: Feb 07, 2010
Posts: 32
posted
0
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
Joined: May 30, 2007
Posts: 68
posted
0
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
This message was edited 1 time. Last update was at by Aleksandar Babic
Anshal Patel
Ranch Hand
Joined: Feb 07, 2010
Posts: 32
posted
0
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
posted
0
issue resolved thanks aleksander
Archana Maharjan
Greenhorn
Joined: Nov 12, 2010
Posts: 5
posted
0
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....
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.
A lot the of modern-day software development platforms are designed to permit parcelling out work to those with the best aptitude for it. A lot of modern-day business is predicated on making one person do all the work, regardless of aptitude.
Archana Maharjan
Greenhorn
Joined: Nov 12, 2010
Posts: 5
posted
0
Thankyou for the quick reply....Okay I'll start a new thread....
subject: How can I recieve data from servlet on j2me application?