A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Mobile
»
Java Micro Edition
Author
Not getting value in midlet from servlet
Kenneth Owino
Greenhorn
Joined: Aug 09, 2010
Posts: 23
posted
May 08, 2011 01:45:16
0
Hi friends. I'm trying to send data from a midlet to a
servlet
and recieve back a response from the servlet but I
don't get any value in response. I've tried to print it on command window and it seems to be null yet I expect only
two values "ok" or "error". Please help me check the code and let me know what I should do to get the response right
on the midlet. My code is below:
import javax.microedition.midlet.*;//midlet class package import import javax.microedition.lcdui.*;//package for ui and commands import javax.microedition.io.*;// import java.io.*; /** * @author k'owino */ //Defining the midlet class public class MvsMidlet extends MIDlet implements CommandListener { private boolean midletPaused = false;//variable for paused state of midlet //defining variables private Display display;// Reference to Display object private Form welForm; private Form vCode;//vote code private StringItem welStr; private TextField phoneField; private StringItem phoneError; private Command quitCmd; private Command contCmd; //constructor of the midlet public MvsMidlet() { display = Display.getDisplay(this);//creating the display object welForm = new Form("THE MVS");//instantiating welForm object welStr = new StringItem("", "Welcome to the MVS, Busitema's mobile voter." + "Please enter the your phone number below");//instantiating welStr string item phoneError = new StringItem("", "");//intantiating phone error string item phoneField = new TextField("Phone number e.g 0789834141", "", 10, 3);//phone number field object quitCmd = new Command("Quit", Command.EXIT, 0);//creating quit command object contCmd = new Command("Continue", Command.OK, 0);//creating continue command object welForm.append(welStr);//adding welcome string item to form welForm.append(phoneField);//adding phone field to form welForm.append(phoneError);//adding phone error string item to form welForm.addCommand(contCmd);//adding continue command welForm.addCommand(quitCmd);//adding quit command welForm.setCommandListener(this); display.setCurrent(welForm); } //start application method definition public void startApp() { } //pause application method definition public void pauseApp() { } //destroy application method definition public void destroyApp(boolean unconditional) { } //Command action method definition public void commandAction(Command c, Displayable d) { if (d == welForm) { if (c == quitCmd) { exitMidlet();//call to exit midlet } else {//if the command is contCmd //place a waiting activity indicator System.out.println("ken de man"); Thread t = new Thread() { public void run() { try { //method to connect to server sendPhone(); } catch (Exception e) { }//end of catch }//end of ru() };//end of thread t.start();//starting the thread }//end of else }//end of first if }//end of command action //defining method to exit midlet public void exitMidlet() { display.setCurrent(null); destroyApp(true); notifyDestroyed(); }//end of exitMidlet() //defining sendPhone method public void sendPhone() throws IOException { System.out.println("ken de man");//check HttpConnection http = null;//HttpConnection variable OutputStream oStrm = null;//OutputStream variable InputStream iStrm = null;//InputStream variable String url = "http://localhost:8084/MvsWeb/CheckPhone";//server url try { http = (HttpConnection) Connector.open(url);//opening connection System.out.println("connection made");//checking code oStrm = http.openOutputStream();//opening output stream http.setRequestMethod(HttpConnection.POST);//setting request type http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//setting content type byte data[] = ("phone=" + phoneField.getString()).getBytes(); oStrm.write(data); iStrm = http.openInputStream();//openning input stream if (http.getResponseCode() == HttpConnection.HTTP_OK) { int length = (int) http.getLength(); String str; if (length != -1) { byte servletData[] = new byte[length]; iStrm.read(servletData); str = new String(servletData); } else // Length not available... { ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); int ch; while ((ch = iStrm.read()) != -1) { bStrm.write(ch); } str = new String(bStrm.toByteArray()); bStrm.close(); } System.out.println("de man"); System.out.println(str); if (str.equals("ok")) { //change to vcode form } else { //add error message to phone_error stingItem } } } catch (Exception e) { } } }//end of class definition //servlet import java.io.*;//package for io classes import javax.servlet.ServletException;//package for servlet exception classes import javax.servlet.http.*; import java.sql.*;//package for sql classes /** * * @author k'owino */ public class CheckPhone extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //declaring variables PrintWriter pw;//PrintWriter object String pnum;//phone parameter sent from client Connection con = null;//connection variable String dbDriver = "com.jdbc.mysql.Driver";//the database driver String dbUrl = "jdbc:mysql://localhost/mvs_db";//the database url String dbUser = "root";//database user String dbPwd = "";//database password PreparedStatement pstmt = null;//Prepared statement variable String query = "select * from student where stud_phone = ?;"; ResultSet rs = null;//resultset variable String dbPhone=null;//phone from database //getting the "phone" parameter sent from client pnum = req.getParameter("phone");//getting the "phone" parameter sent from client System.out.println(pnum); //setting the content type of the response res.setContentType("text/html"); //creating a PrintWriter object pw = res.getWriter(); pw.println("ken"); try{//trying to load the database driver and establish a connection to the database Class.forName(dbDriver).newInstance();//loading the database driver con = DriverManager.getConnection(dbUrl,dbUser,dbPwd);//establishing a connection to the database System.out.println("connection established"); } catch(Exception e){ } //trying to query the database try{ pstmt = con.prepareStatement(query);//preparing a statement pstmt.setString(1, pnum);//setting the input parameter rs = pstmt.executeQuery();//executing the query assigning to the resultset object //extracring data from the resultset while(rs.next()){ dbPhone = rs.getString("stud_phone");//getting the phone number from database }//end of while if(pnum.equals(dbPhone)){ pw.print("ok"); pw.close(); } else{ pw.print("error"); pw.close(); } } catch(Exception e){ } } @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8441
I like...
posted
May 09, 2011 11:09:04
0
catch(Exception e){
}
If I were you, I would start by changing it to
catch(Exception e){
e.printStackTrace();
}
[
Donate a pint, save a life!
] [
How to ask questions
] [
Onff-turn it on!
]
Soumya Rout
Ranch Hand
Joined: Aug 06, 2009
Posts: 49
I like...
posted
May 10, 2011 06:29:40
0
Try this
http = (HttpConnection) Connector.open(url+"?phone="+phoneField.getString();
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Not getting value in midlet from servlet
Similar Threads
failing to get login right from servlet and midlet
Space character in URL error when running midlet
Command buttons display on screen
How to invoke the MIDlet ?
User-Agent is not being send + midlet
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter