扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:佚名 来源:BLOG 2007年11月21日
关键字:
import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; import javax.microedition.lcdui.*; public class getHttp extends MIDlet { public void startApp() { try { //打开网络连接 String url = "http://127.0.0.1/515game/myweb.html"; StreamConnection sc = (StreamConnection) Connector.open(url); //读取数据 InputStream is = sc.openInputStream(); int tmp = 0; String get = ""; while (tmp != -1) { //-1代表结束 tmp = is.read(); get = get + (char) tmp; } is.close(); Form f = new Form(url); //解决中文问题 String chinese = new String(get.getBytes("iso8859-1"), "utf-8"); f.append(chinese); Display.getDisplay(this).setCurrent(f); //关闭网络连接 sc.close(); } catch (Exception e) {} } public void pauseApp() {} public void destroyApp(boolean f) {} } |
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; public class testPic extends MIDlet { public void startApp() { try { //打开网络连接 String url = "http://127.0.0.1/515game/back0.png"; StreamConnection sc = (StreamConnection) Connector.open(url); //获取图片 InputStream is = sc.openInputStream(); Image im = Image.createImage(is);//该方法为MIDP 2.0方法 Form f = new Form(url); f.append(im); Display.getDisplay(this).setCurrent(f); //关闭连接 sc.close(); } catch (Exception e) {} } public void pauseApp() {} public void destroyApp(boolean f) {} } |
import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.io.*; import java.io.*; import javax.microedition.media.*; public class getMusic extends MIDlet { public void startApp() { try { //打开网络连接 String url = "http://127.0.0.1/515game/kk.wav"; StreamConnection sc = (StreamConnection) Connector.open(url); //获取声音 InputStream is = sc.openInputStream(); Player p1 = Manager.createPlayer(is, "audio/x-wav"); p1.start(); //关闭网络连接 sc.close(); System.out.println("sound is play"); } catch (Exception e) { e.printStackTrace(); } } public void pauseApp() {} public void destroyApp(boolean f) {} } |
服务器端程序 checkuser.jsp 这个文件放到d:/ mygameWeb目录下面 <% //得到用户名 String name=request.getParameter("username"); //得到密码 String pass=request.getParameter("password"); if(name.equals("ldh")) { if(pass.equals("zhm")) { out.print("welcome "); } else { out.print("pass word error"); } } else { out.print("user name error"); } %> 客户端程序 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; public class logoIN extends MIDlet implements CommandListener, Runnable { public Form f; public TextField user; //用户名 public TextField pass; //密码 public Command c1; public logoIN() { f = new Form("传奇世界用户登陆"); user = new TextField("用户名", "", 10, TextField.ANY); pass = new TextField("密码", "", 8, TextField.PASSWORD); f.append(user); f.append(pass); c1 = new Command("确定", Command.SCREEN, 1); f.addCommand(c1); f.setCommandListener(this); Display.getDisplay(this).setCurrent(f); } public void startApp() {} public void pauseApp() {} public void destroyApp(boolean f) {} public void commandAction(Command c, Displayable dd) { Thread t = new Thread(this); t.start(); //启动线程连结网络 } //完成网络请求 public void run() { try { //打开网络 String url = "http://127.0.0.1/515game/checkuser.jsp?username=" + user.getString() + "&password=" + pass.getString(); //获取数据 StreamConnection sc = (StreamConnection) Connector.open(url); InputStream is = sc.openInputStream(); int tmp = 0; String get = ""; while ( (tmp = is.read()) != -1) { get = get + (char) tmp; } Form f2 = new Form("登陆结果"); f2.append(get); Display.getDisplay(this).setCurrent(f2); //关闭网络 sc.close(); } catch (Exception e) {} } } |
<% String sjg=request.getParameter("jg"); int jg=(int)(Math.random()*10); int userjg=Integer.parseInt(sjg); if(userjg>jg) { out.println("sorry da le"); } if(userjg<jg) { out.println("sorry xiao le"); } if(userjg==jg) { out.println("right"); } %> J2me客户端代码: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; public class GuessGame extends MIDlet implements CommandListener, Runnable { public Form f, f2; public TextField tf1; public Display d; public Command c1, c2; public GuessGame() { f = new Form("商品竞猜"); f2 = new Form("竞猜结果"); c2 = new Command("返回", Command.SCREEN, 1); f2.addCommand(c2); f2.setCommandListener(this); tf1 = new TextField("请输入商品价格1-9", "", 1, TextField.NUMERIC); f.append(tf1); c1 = new Command("确定", Command.SCREEN, 1); f.addCommand(c1); f.setCommandListener(this); d = Display.getDisplay(this); } public void startApp() { d.setCurrent(f); } public void pauseApp() {} public void destroyApp(boolean f) {} public void commandAction(Command c, Displayable d) { if (c == c1) { //启动网络请求 (new Thread(this)).start(); } if (c == c2) { this.d.setCurrent(f); } } public void run() { try { //打开网络连接 String url = "http://127.0.0.1/515game/Guess.jsp?jg="+ tf1.getString(); StreamConnection sc = (StreamConnection) Connector.open(url); //获取请求结果 InputStream is = sc.openInputStream(); int tmp = 0; String get = ""; while ( (tmp = is.read()) != -1) { get = get + (char) (tmp); } for (int i = 0; i < f2.size(); i++) { f2.delete(i); } f2.append(get); d.setCurrent(f2); //关闭网络连接 sc.close(); } catch (Exception e) {} } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者