Solved

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

Posted on 2004-09-12
7
326 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
ID: 12072769
int i;
                while ((i = is.read()) != -1) {
                   buffer += (char)i;      
                }

                is.close();
0
 

Expert Comment

by:thomasbau
ID: 12072789
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
ID: 12081940
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
ID: 12090820
Think about the serverside application you want to worke with first.
:-)
0
 

Author Comment

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

Featured Post

Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
This subject  of securing wireless devices conjures up visions of your PC or mobile phone connecting to the Internet through some hotspot at Starbucks. But it is so much more than that. Let’s look at the facts: devices#sthash.eoFY7dic.
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now