Results 1 to 1 of 1
- 05-09-2011, 06:45 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Not getting value in midlet from servlet
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:
Java Code: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> }
Similar Threads
-
Cant pass ByteArray from Servlet to Midlet
By Anahcra in forum CLDC and MIDPReplies: 0Last Post: 10-28-2010, 09:03 AM -
How to pass value from servlet to midlet?
By sharmaj2me in forum CLDC and MIDPReplies: 1Last Post: 12-05-2008, 03:52 PM -
Connectivity between Servlet and Midlet
By adarsh_2484 in forum New To JavaReplies: 1Last Post: 07-29-2008, 04:09 PM -
MIDLet calling Servlet
By ravian in forum Java ServletReplies: 1Last Post: 12-06-2007, 12:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks