使用HTTP协议发送和接受文本数据 SampleClient.java

本文介绍了一个使用Java ME开发的简单客户端应用程序示例,该应用通过HTTP POST方式发送用户输入的数据到服务器,并接收服务器响应。示例涵盖了如何创建用户界面、处理HTTP请求及响应等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.HttpConnection;
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.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class SampleClient extends MIDlet implements CommandListener {

private static String url = "http://localhost:8080/servlet/j2me.techtips.SampleServer";
private Display display;

private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Command okCommand = new Command("OK",Command.EXIT,1);
private Command sendCommand = new Command("Send",Command.EXIT,1);

private TextBox entryForm;

public SampleClient(){
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
exitMIDlet();
}

protected void pauseApp() {
}

protected void startApp() throws MIDletStateChangeException {
if(display == null){
initMIDlet();
}
}

private void initMIDlet(){
display = Display.getDisplay(this);
entryForm = new EntryForm();
display.setCurrent(entryForm);
}

private void exitMIDlet(){
notifyDestroyed();
}

public void commandAction(Command c, Displayable d) {
if(c == sendCommand){
StatusForm f = new StatusForm(entryForm.getString());
display.setCurrent(f);
f.start();
}else if(c == okCommand){
display.setCurrent(entryForm);
}else{
exitMIDlet();
}
}

class EntryForm extends TextBox {
EntryForm(){
super("Enter some text","",80,0);
addCommand(exitCommand);
addCommand(sendCommand);
setCommandListener(SampleClient.this);
}
}

class StatusForm extends Form implements Runnable ,HttpConnectionHelper.Callback {

private StringItem message;
private byte[] data;

StatusForm (String text){
super("Statue");

try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);

dout.writeUTF(text);
data = bout.toByteArray();

dout.close();
} catch (IOException e) {
e.printStackTrace();
}
}

void display(String text){
if(message == null){
message = new StringItem(null,text);
append(message);
}else{
message.setText(text);
}
}

void done(String msg){
display(msg!=null?msg:"Done.");
addCommand(okCommand);
setCommandListener(SampleClient.this);
}

public void prepareRequest(String originalURL,HttpConnection conn ) throws IOException{
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Accept", "application/octet-stream");
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Content-Length", Integer.toString(data.length));

OutputStream os = conn.openOutputStream();
os.write(data);
os.close();
}

public void run(){
HttpConnection conn = null;

display("Obtaining HttpConnection object...");

try {
conn = HttpConnectionHelper.connect(url,this);

display("Connecting to the server...");
int rc = conn.getResponseCode();

if(rc == HttpConnection.HTTP_OK){
StringBuffer text = new StringBuffer();

try {
DataInputStream din = new DataInputStream(conn.openInputStream());

int n = din.readInt();
while(n-->0){
text.append(din.readUTF());
text.append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

void start(){
display("Starting...");

Thread t = new Thread(this);

try {
t.start();
} catch (Exception e) {
done("Exception " + e + " trying to start thread.");
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值