ZT----J2me网络编程以及网络游戏的实现

ZT 8)

个人觉得这几个例子对于理解J2ME网络编程还是很有帮助的,希望对需要的人有所帮助:D


二、从Web获取文字信息

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) {}



}

三、从Web获取图片信息

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) {}

}

四、从Web获取多媒体信息

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) {}



}



五、基于http的用户登陆系统实现

服务器端程序

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) {}

}



}



六、一个网络游戏实例

下面我们通过一个网络猜价格的游戏实例来说明如何通过J2me同服务器端交换数据。

这是一个网络版商品竞猜的实例,客户端输入商品价格,在服务器端负责游戏逻辑的处理。

服务器端代码:

Guess.jsp 这个文件放到d:/ mygameWeb目录下面

<%

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) {}

}

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sdhjob/archive/2006/09/14/1221743.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值