0

我是 J2ME 的新手,正在构建一个移动应用程序,用户可以在其中注册、登录并显示一些仅限会员的屏幕,他们可以在其中执行一些操作。

(我从事网络开发,因此请纠正我在哪里使用网络方法。另外,我清楚地了解基本的 Java 概念,但之前没有构建过移动应用程序)

到目前为止,我已经创建了第一个屏幕,其中包含一个包含用户名和密码文本框以及登录和退出按钮的登录表单。

我目前面临的问题是:

  1. 如何同时提供注册、登录和退出的访问权限(手机似乎一次只能有两个按钮)。我是否将它们作为命令按钮或普通的“出现在页面上的类似 web 的按钮”提供?请告诉我如何使用任何似乎合适的选项。

  2. 如何在移动应用程序中通过 http 发送和接收数据?示例代码将不胜感激。

  3. 如何管理不同的屏幕?在 Web 开发中,我只是创建页面并将它们链接起来。在这种情况下,如何在按下注册按钮时显示注册屏幕?登录成功时的主屏幕?或不成功时的错误消息?

我有动态生成屏幕的不同功能吗?每次请求屏幕时我都会打电话给他们?

简单的样品将受到高度赞赏。

4

1 回答 1

1

在 J2ME 中,

你有一个显示对象,它决定了屏幕上显示的内容。有些东西可以显示。例如表单、列表、文本框等。因此,您可以创建以下显示

public class IndexScreen extends List implements CommandListener {

    //This will contain the options 
    //1. Register
    //2. Login
    //3. Exit
}


public class RegsterScreen extends Form implements CommandListener {

    //This will contain register fields and submit cancel command buttons

}

public class LoginScreen extends Form implements CommandListener {

   //This will contain Login specific controls
}

现在,一旦这些可显示对象准备就绪,您就可以继续更改某些事件的显示,例如单击命令按钮。

public void commandAction(Command c, Displayable d) {

    if (c == OK) {
        nextScreen = new RegisterScreen();
        display.setCurrent(nextScreen);
    }
    if (c == BACK) {
        display.setCurrent(prevScreen);
    }

对于发送和接收数据,以下可能会有所帮助....

package madmin.client;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Display;
import madmin.res.Globals;


public class ClientRequest {

private Client client;
private Display display;
private String requestServlet;
private String requestCode;
private String requestId;
private String userId;
private String url;
private String response;
private String parameterOne;



public ClientRequest() {

}

 public boolean sendRequest() {

      boolean result = false;
      userId = Globals.getUserId();
      url = Globals.getURL() + requestServlet + "?requestCode=" + requestCode + "&requestId=" + requestId + "&userId=" + userId + "&clientIP=" + client.getIpAddress() + "&clientHostName=" + client.getHostname() + "&parameterOne=" + parameterOne;

      System.out.println("User Id value in ClientRequest " + userId);


      System.out.println("Start HTTP Connection");
      HttpConnection connection = null;
      InputStream inputstream = null;

       try {
            connection = (HttpConnection) Connector.open(url);
            connection.setRequestMethod(HttpConnection.GET);
            connection.setRequestProperty("Content-Type", "//text plain");
            connection.setRequestProperty("Connection", "close");

            System.out.println("Status Line COde: "+ connection.getResponseCode());
            System.out.println("Status Line Message: "+ connection.getResponseMessage());


            if(connection.getResponseCode()==HttpConnection.HTTP_OK){
                inputstream = connection.openInputStream();
                int length = (int) connection.getLength();
                if(length!=-1){
                    byte incomingData[] = new byte[length];
                    inputstream.read(incomingData);
                    response = new String(incomingData);
                }
                else {
                    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                    int ch;
                    while((ch = inputstream.read())!=-1){
                        bytestream.write(ch);
                    }
                    response = new String(bytestream.toByteArray());
                    bytestream.close();
                }

               System.out.println("Response:" + response.trim());

               if(response.trim().equals("Request Submitted Successfully")){
                       result = true;
               }
               else{
                    result = false;
               }
            }
            connection.close();
            if(inputstream!=null)inputstream.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            if(inputstream!=null){
                try{
                    inputstream.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
     return result;
 }

public Client getClient() {
    return client;
}

public void setClient(Client client) {
    this.client = client;
}

public Display getDisplay() {
    return display;
}

public void setDisplay(Display display) {
    this.display = display;
}

public String getRequestCode() {
    return requestCode;
}

public void setRequestCode(String requestCode) {
    this.requestCode = requestCode;
}

public String getRequestId() {
    return requestId;
}

public void setRequestId(String requestId) {
    this.requestId = requestId;
}

public String getRequestServlet() {
    return requestServlet;
}

public void setRequestServlet(String requestServlet) {
    this.requestServlet = requestServlet;
}

public String getResponse() {
    return response;
}

public void setResponse(String response) {
    this.response = response;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getParameterOne() {
    return parameterOne;
}

public void setParameterOne(String parameterOne) {
    this.parameterOne = parameterOne;
}

}

和....

package madmin.client;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import madmin.res.Globals;


public class ClientResponse {

private String response;


public String getResponse(String requestId) {

         System.out.println("Start HTTP Connection");
         HttpConnection connection = null;
         InputStream inputstream = null;
         response = "";

          try{

            connection = (HttpConnection) Connector.open( Globals.getURL() + "ResponseServlet?requestId=" + requestId);
            connection.setRequestMethod(HttpConnection.GET);
            connection.setRequestProperty("Content-Type", "//text plain");
            connection.setRequestProperty("Connection", "close");

            System.out.println("Status Line COde: "+ connection.getResponseCode());
            System.out.println("Status Line Message: "+ connection.getResponseMessage());

            if(connection.getResponseCode()==HttpConnection.HTTP_OK){
                inputstream = connection.openInputStream();
                int length = (int) connection.getLength();
                if(length!=-1){
                    byte incomingData[] = new byte[length];
                    inputstream.read(incomingData);
                    response = new String(incomingData);
                }
                else {
                    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                    int ch;
                    while((ch = inputstream.read())!=-1){
                        bytestream.write(ch);
                    }
                    response = new String(bytestream.toByteArray());
                    bytestream.close();
                }

               System.out.println("Response:" + response.trim());

              connection.close();
              if(inputstream!=null)inputstream.close();
            }
            }
            catch(Exception e){

                e.printStackTrace();
            }

    return response;
}
}

编辑:像这样

     public class MenuScreen extends List implements CommandListener{

     public MenuScreen() {
 append("Register", null);
     append("Login", null);
     append("Forgot password", null);


     select = new Command("Select", Command.OK, 1);
     addCommand(select);
     setCommandListener(this);

}

}

     public void commandAction(Command command, Displayable display) {

      if(command==List.SELECT_COMMAND){                 
        String menuItem = this.getString(menuIndex);
      }

      if(command==select){       
        if( menuItem.equals("Register"))
     display.setCurrent(new RegisterScreen())
 }
      }
于 2012-05-07T06:55:54.213 回答