?? obexclient.java
字號:
package com.j2medev.chapter9;
import java.io.*;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.*;
import javax.obex.*;
public class OBEXClient implements Runnable,CommandListener{
private OBEXMIDlet midlet = null;
private ClientSession conn = null;
private Form form = new Form("client");
private Account account = null;
private Command sendCommand = new Command("send",Command.OK,1);
public OBEXClient(OBEXMIDlet _midlet) {
midlet = _midlet;
//創建一個Account
account = new Account("mingjava","helloj2me");
form.append("username="+account.getUsername()+"\n");
form.append("password="+account.getPassword()+"\n");
form.append("want to send this object?click send command");
form.addCommand(sendCommand);
form.setCommandListener(this);
midlet.setCurrent(form);
}
public void run() {
try{
conn = (ClientSession)Connector.open("tcpobex://localhost:5000");
//連接到服務器
form.append("connect to obex server...\n");
HeaderSet resp = conn.connect(null);
if(resp.getResponseCode() != ResponseCodes.OBEX_HTTP_OK){
//如果連接失敗則返回
form.append("can not connect to server");
return;
}
//向服務器發送對象,首先創建PUT請求
HeaderSet header = conn.createHeaderSet();
header.setHeader(HeaderSet.LENGTH,new Long(account.getLength()));
Operation op = conn.put(header);
//發送Account
DataOutputStream dos = op.openDataOutputStream();
dos.writeUTF(account.getUsername());
dos.writeUTF(account.getPassword());
//退出
dos.close();
op.close();
form.deleteAll();
form.append("the object has been sent to server");
form.addCommand(new Command("back",Command.BACK,1));
}catch(IOException ex){
ex.printStackTrace();
return;
}
}
public void commandAction(Command command, Displayable displayable) {
if(command == sendCommand){
form.deleteAll();
//啟動線程,發送對象
new Thread(this).start();
}else if(command.getCommandType() == Command.BACK){
midlet.show();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -