Solved

StreamConnection, How can i pass my data to HTTP from MIDlet?

Posted on 2004-09-12
7
324 Views
Last Modified: 2008-01-09
Dear Experts,

Im learning bout StreamConnection in J2ME, I want to pass my data that is username and password from my MIDlet to http.
Im using j2sdk1.4.1 dan WTK2.1. In emulation after message "form wants to send and receive data using the network. This will use airtime and mau result in charge. Is it OK to use Airtime?" then i choose "Yes" there is no work. What happen? i don't know.

Then, how can i pass the data from Midlet to HTTP?
Here is my code : (please give me correction and suggestion)

---------
form.java
----------
import java.util.*;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class form extends MIDlet implements CommandListener{
      
       private Display display;
     private Form frmLogin;
       private TextField username;
       private TextField password;

     public  form(){
        display    = Display.getDisplay(this);
            frmLogin = new Form("Login");
            username = new TextField(" User Name","",25,TextField.ANY);
            password  = new TextField(" Password  ","",45,TextField.ANY|TextField.PASSWORD);

            Command cmdLogin = new Command("Login",Command.OK,1);
       
            frmLogin.append(username);
            frmLogin.append(password);
            frmLogin.addCommand(cmdLogin);
            frmLogin.setCommandListener(this);
     }
     
     public void startApp() throws MIDletStateChangeException{         
            display.setCurrent(frmLogin);
     }

     public void pauseApp() {
       }

     public void destroyApp(boolean unconditional){
          notifyDestroyed();
       }

       public void login(){
            String kata = "No Result";
            StreamConnection conn = null;
            InputStream in = null;
            StringBuffer data = new StringBuffer();
            try{
               //data yang akan dikirim adalah username dan password
               conn = (StreamConnection)Connector.open("http://localhost/login/process.asp?q=login&tUserId="+username.getString()+"&tPassword="+password.getString());

               in = conn.openInputStream();

               /*
                     What should be coded here? Plz help...:-(
                     ????
                   */
            }
            catch(IOException e){
                  System.err.println("The Connection cannot be established");
            }
       }

       public void commandAction(Command c, Displayable d){
             String label = c.getLabel();
             if (label.equals("Login")) login();
       }
}

-----------
process.asp
-----------
<!--#include file="function.asp"-->
<%
response.buffer = true
if request("q") = "login" then
   if valUser(request("tUserId"),request("tPassword")) then
      session("userid")=request("tuserId")
        response.Write("welcome")
   else
      response.write "unregistered"
end if
%>

-----------
function.asp
-----------
<%
response.buffer = true
function checkUserID(userid)
   sql = "SELECT * FROM user WHERE user_id = '"&userid&"'"
   %><!--#include file="opendb.inc"--><%
   if rs.eof then checkUserID = false else checkUserID = true  
end function

function valUser(userid,password)
   sql = "SELECT * FROM user WHERE user_id = '"&userid&"' AND password = '"&password&"'"
   %><!--#include file="opendb.inc"--><%
   if rs.eof then valUser = false else valUser = true  
end function
%>

------------
opendb.inc
------------
<%
  openDB = "driver={Microsoft Access Driver (*.mdb)};" & "dbq=" & server.mapPath("data.mdb")
  set rs = server.createObject("ADODB.recordSet")
  rs.Open sql, openDB, 2, 3
%>
0
Comment
Question by:ahmed_x
  • 3
  • 2
7 Comments
 

Accepted Solution

by:
thomasbau earned 125 total points
Comment Utility
int i;
                while ((i = is.read()) != -1) {
                   buffer += (char)i;      
                }

                is.close();
0
 

Expert Comment

by:thomasbau
Comment Utility
the thing just jumped off, sorry  here the details:

"is" is your inputstream
"buffer" is a String

I hope this helps
greetings
0
 

Author Comment

by:ahmed_x
Comment Utility
ok, thank you so much, i got it...:-)
Well are there any ideas from all of you experts to develop any simple aplication using HTTP Connections? please ...
0
 

Expert Comment

by:thomasbau
Comment Utility
Think about the serverside application you want to worke with first.
:-)
0
 

Author Comment

by:ahmed_x
Comment Utility
:-o
Serverside application?
What kind of it?
I'm using Web based Application and ASP for it.
:-l
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wireless Troubleshooting 7 75
Wifi extender 3 78
Dell 3115cn - SMTP error 016-764 16 316
NPS running on Win 2012 R2 2 30
Multi-source agreements are important because they set standards that all manufacturers should follow to ensure that devices are compatible with multiple vendors. The multi-source agreement (MSA) is an agreement that establishes how multiple vendors…
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now