1.涉及网络:
1.Socket
2.UDP
3.HTTP
2.主要操作对象
此三个对象一般都是由父接口Connection的一个实例强转而成。
例如:Socket:
SocketConnection scc=(SocketConnection)Connector.open("socket://:8787");//服务器端侦听
UDP:
UDPDatagramConnection udc=(UDPDatagramConnection)Connector.open("datagram://:8787");
HTTP:
HttpConnection hc=(HttpConnection)Connector.open("http://localhost:81/a.php");
3.简单DEMO实例
3.1 Socket
ServletMIdet.class
ClientMIDlet.class
效果: 客户端与服务端进行通话 显示各自内容1.Socket
2.UDP
3.HTTP
2.主要操作对象
此三个对象一般都是由父接口Connection的一个实例强转而成。
例如:Socket:
SocketConnection scc=(SocketConnection)Connector.open("socket://:8787");//服务器端侦听
UDP:
UDPDatagramConnection udc=(UDPDatagramConnection)Connector.open("datagram://:8787");
HTTP:
HttpConnection hc=(HttpConnection)Connector.open("http://localhost:81/a.php");
3.简单DEMO实例
3.1 Socket
ServletMIdet.class
Code
package org;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ServerMIDlet extends MIDlet implements CommandListener, Runnable {
ServerSocketConnection ssc;
SocketConnection sc;
DataInputStream dis;
DataOutputStream dos;
private Display display;
private TextField txtMsg = new TextField("信息", "", 100, TextField.ANY);
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private Form form = new Form("服务器端");
public ServerMIDlet() {
display = Display.getDisplay(this);
form.addCommand(cmdSend);
form.append(txtMsg);
form.setCommandListener(this);
display.setCurrent(form);
}
// 此程序要有客户端连接后才开启
protected void startApp() throws MIDletStateChangeException {
try {
ssc = (ServerSocketConnection) Connector.open("socket://:9999");
sc = (SocketConnection) ssc.acceptAndOpen();//等待客户端连接 如果没有客户端连接 将会阻塞
dis = sc.openDataInputStream();
dos = sc.openDataOutputStream();
new Thread(this).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
try {
// 服务器端发送给客户端信息
dos.writeUTF(txtMsg.getString());
System.out.println("Server Send Success!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 显示客户端发过来的信息 因为不明客户端何时发过来 所以采用线程
public void run() {
while (true) {
try {
form.append(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
try {
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void pauseApp() {
}
}
package org;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ServerMIDlet extends MIDlet implements CommandListener, Runnable {
ServerSocketConnection ssc;
SocketConnection sc;
DataInputStream dis;
DataOutputStream dos;
private Display display;
private TextField txtMsg = new TextField("信息", "", 100, TextField.ANY);
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private Form form = new Form("服务器端");
public ServerMIDlet() {
display = Display.getDisplay(this);
form.addCommand(cmdSend);
form.append(txtMsg);
form.setCommandListener(this);
display.setCurrent(form);
}
// 此程序要有客户端连接后才开启
protected void startApp() throws MIDletStateChangeException {
try {
ssc = (ServerSocketConnection) Connector.open("socket://:9999");
sc = (SocketConnection) ssc.acceptAndOpen();//等待客户端连接 如果没有客户端连接 将会阻塞
dis = sc.openDataInputStream();
dos = sc.openDataOutputStream();
new Thread(this).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
try {
// 服务器端发送给客户端信息
dos.writeUTF(txtMsg.getString());
System.out.println("Server Send Success!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 显示客户端发过来的信息 因为不明客户端何时发过来 所以采用线程
public void run() {
while (true) {
try {
form.append(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
try {
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void pauseApp() {
}
}
ClientMIDlet.class
Code
package org;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ClientMIDlet extends MIDlet implements CommandListener {
SocketConnection sc;
DataInputStream dis;
DataOutputStream dos;
private Display display;
private TextField txtMsg = new TextField("信息", "", 100, TextField.ANY);
private Command cmdSend = new Command("发送", Command.SCREEN, 0);
private Form form = new Form("客户端");
public ClientMIDlet() {
display = Display.getDisplay(this);
form.addCommand(cmdSend);
form.append(txtMsg);
form.setCommandListener(this);
display.setCurrent(form);
}
protected void startApp() throws MIDletStateChangeException {
try {
sc = (SocketConnection) Connector.open("socket://127.0.0.1:9999");
dos = sc.openDataOutputStream();
dis = sc.openDataInputStream();
new ReciveThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
try {
// 发送给服务器端信息
dos.writeUTF(txtMsg.getString());
System.out.println("Client Send Success!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class ReciveThread extends Thread {
public void run() {
while (true) {
try {
form.append(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package org;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ClientMIDlet extends MIDlet implements CommandListener {
SocketConnection sc;
DataInputStream dis;
DataOutputStream dos;
private Display display;
private TextField txtMsg = new TextField("信息", "", 100, TextField.ANY);
private Command cmdSend = new Command("发送", Command.SCREEN, 0);
private Form form = new Form("客户端");
public ClientMIDlet() {
display = Display.getDisplay(this);
form.addCommand(cmdSend);
form.append(txtMsg);
form.setCommandListener(this);
display.setCurrent(form);
}
protected void startApp() throws MIDletStateChangeException {
try {
sc = (SocketConnection) Connector.open("socket://127.0.0.1:9999");
dos = sc.openDataOutputStream();
dis = sc.openDataInputStream();
new ReciveThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
try {
// 发送给服务器端信息
dos.writeUTF(txtMsg.getString());
System.out.println("Client Send Success!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class ReciveThread extends Thread {
public void run() {
while (true) {
try {
form.append(dis.readUTF());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
3.2 UDP
UDP(只管发送 不管发送状态 进行Datagram数据报传输) 此DEMO实现效果同上
ServerMIDlet.class
Code
package com;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.UDPDatagramConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ServletMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("");
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private TextField txtMsg = new TextField("信息", "", 50, TextField.ANY);
private UDPDatagramConnection udc;
private Datagram dg;
private final int MAXSIZE=50;
private String address;
public ServletMIDlet() {
display = Display.getDisplay(this);
form.append(txtMsg);
form.addCommand(cmdSend);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
byte[] b = txtMsg.getString().getBytes();
try {
//服务器端发给客户端要设置地址
dg = udc.newDatagram(b, b.length,address);
udc.send(dg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void startApp() throws MIDletStateChangeException {
try {
udc=(UDPDatagramConnection)Connector.open("datagram://:8787");
new MsgDisplayThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
class MsgDisplayThread extends Thread{
public void run()
{
try {
while(true)
{
//构建数据报 要给以长度
dg=udc.newDatagram(MAXSIZE);
//收到数据报
udc.receive(dg);
address=dg.getAddress();
//读取内容
form.append(new String(dg.getData())+"\n");
} }catch (IOException e) {
e.printStackTrace();
}
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
package com;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.UDPDatagramConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ServletMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("");
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private TextField txtMsg = new TextField("信息", "", 50, TextField.ANY);
private UDPDatagramConnection udc;
private Datagram dg;
private final int MAXSIZE=50;
private String address;
public ServletMIDlet() {
display = Display.getDisplay(this);
form.append(txtMsg);
form.addCommand(cmdSend);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
byte[] b = txtMsg.getString().getBytes();
try {
//服务器端发给客户端要设置地址
dg = udc.newDatagram(b, b.length,address);
udc.send(dg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void startApp() throws MIDletStateChangeException {
try {
udc=(UDPDatagramConnection)Connector.open("datagram://:8787");
new MsgDisplayThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
class MsgDisplayThread extends Thread{
public void run()
{
try {
while(true)
{
//构建数据报 要给以长度
dg=udc.newDatagram(MAXSIZE);
//收到数据报
udc.receive(dg);
address=dg.getAddress();
//读取内容
form.append(new String(dg.getData())+"\n");
} }catch (IOException e) {
e.printStackTrace();
}
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
ClientMIDlet.class
Code
package com;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.UDPDatagramConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ClientMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("");
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private TextField txtMsg = new TextField("信息", "", 50, TextField.ANY);
private UDPDatagramConnection udc;
private Datagram dg;
private final int MAXSIZE = 50;
public ClientMIDlet() {
display = Display.getDisplay(this);
form.append(txtMsg);
form.addCommand(cmdSend);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
byte[] b = txtMsg.getString().getBytes();
try {
// 构建数据报
dg = udc.newDatagram(b, b.length);
// 发送数据报
udc.send(dg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void startApp() throws MIDletStateChangeException {
try {
udc = (UDPDatagramConnection) Connector
.open("datagram://127.0.0.1:8787");
new MsgDisplayThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class MsgDisplayThread extends Thread {
public void run() {
try {
while (true) {
dg = udc.newDatagram(MAXSIZE);
udc.receive(dg);
form.append(new String(dg.getData()) + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.UDPDatagramConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class ClientMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form form = new Form("");
private Command cmdSend = new Command("发送", Command.SCREEN, 1);
private TextField txtMsg = new TextField("信息", "", 50, TextField.ANY);
private UDPDatagramConnection udc;
private Datagram dg;
private final int MAXSIZE = 50;
public ClientMIDlet() {
display = Display.getDisplay(this);
form.append(txtMsg);
form.addCommand(cmdSend);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == cmdSend) {
byte[] b = txtMsg.getString().getBytes();
try {
// 构建数据报
dg = udc.newDatagram(b, b.length);
// 发送数据报
udc.send(dg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void startApp() throws MIDletStateChangeException {
try {
udc = (UDPDatagramConnection) Connector
.open("datagram://127.0.0.1:8787");
new MsgDisplayThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
class MsgDisplayThread extends Thread {
public void run() {
try {
while (true) {
dg = udc.newDatagram(MAXSIZE);
udc.receive(dg);
form.append(new String(dg.getData()) + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.3 HTTP
a .php(模拟验证用户名和密码)
<?php
$userName=$_GET["userName"];
$pwd=$_GET["pwd"];
if($userName!="zhang"||$pwd!="123")
echo "error!";
else
echo "welcome {$userName}!";
?>
MainMIDlet.class
$userName=$_GET["userName"];
$pwd=$_GET["pwd"];
if($userName!="zhang"||$pwd!="123")
echo "error!";
else
echo "welcome {$userName}!";
?>
Code
package org;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MainMIDlet extends MIDlet {
public MainMIDlet() {
}
protected void startApp() throws MIDletStateChangeException {
try {
String url = "http://localhost:81/a.php?userName=zhang&pwd=123";
HttpConnection hc = (HttpConnection) Connector.open(url);
DataInputStream dis = hc.openDataInputStream();
// 读取该URL指定的页面内容
byte[] b = new byte[100];
dis.read(b);
dis.close();
String str = new String(b);
System.out.println("page content:" + str);
} catch (IOException e) {
e.printStackTrace();
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
package org;
import java.io.DataInputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MainMIDlet extends MIDlet {
public MainMIDlet() {
}
protected void startApp() throws MIDletStateChangeException {
try {
String url = "http://localhost:81/a.php?userName=zhang&pwd=123";
HttpConnection hc = (HttpConnection) Connector.open(url);
DataInputStream dis = hc.openDataInputStream();
// 读取该URL指定的页面内容
byte[] b = new byte[100];
dis.read(b);
dis.close();
String str = new String(b);
System.out.println("page content:" + str);
} catch (IOException e) {
e.printStackTrace();
}
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
控制台输出:page content:welcome zhang!
其他
1、打开文件方法,建立文件连接进行IO通讯
Connection conn = Connector.open(file://myfile.dat);
2、调用串口通讯的建立方法
Connection conn = Connector.open("comm:0; baudrate=5333");