gpt4 book ai didi

java - 在 J2ME 中通过 http 发送和接收数据以及处理菜单和屏幕

转载 作者:行者123 更新时间:2023-12-01 15:23:48 25 4
gpt4 key购买 nike

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

(我热衷于 Web 开发,因此请纠正我使用 Web 方法的地方。此外,我清楚地了解基本的 Java 概念,但之前没有构建过移动应用程序)

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

我目前面临的问题是:

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

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

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

我有不同的函数来动态生成屏幕吗?每次需要屏幕时我都会调用他们?

简单的示例将受到高度赞赏。

最佳答案

在 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)无效commandAction(命令c,可显示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())
}
}

关于java - 在 J2ME 中通过 http 发送和接收数据以及处理菜单和屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com