⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpiqtest.java

📁 手机联网考试系统 由于多因素影响
💻 JAVA
字号:
package com.chapter15;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

import java.io.*;

public class HttpIQTest extends MIDlet implements CommandListener {
	private Display display;

	private Form mainForm;

	private Command exitCommand;

	private Command connectCommand;

	private Command backCommand;

	private Command sendCommand;

	private TextField useridField;

	private TextField usernameField;

	private String userid;

	private String username;

	public HttpIQTest() {
		display = Display.getDisplay(this);

		exitCommand = new Command("Exit", Command.EXIT, 1);
		connectCommand = new Command("Connect", Command.SCREEN, 1);
		backCommand = new Command("Back", Command.BACK, 1);
		sendCommand = new Command("Send", Command.SCREEN, 1);

		mainForm = new Form("服务器的信息");

		useridField = new TextField("输入用户编号", null, 25, TextField.NUMERIC);
		usernameField = new TextField("输入用户姓名", null, 25, TextField.ANY);

		mainForm.append(useridField);
		mainForm.append(usernameField);

		mainForm.addCommand(exitCommand);
		mainForm.addCommand(connectCommand);
		mainForm.setCommandListener(this);

	}

	public void startApp() {
		display.setCurrent(mainForm);

	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
	}

	public void getQuestion() throws IOException {
		HttpConnection conn = null;

		InputStream iStrm = null;

		String url = "http://127.0.0.1:8080/TEst/IQTest";

		Form f;
		f = new Form("请解答");

		f.addCommand(backCommand);
		f.addCommand(sendCommand);
		f.setCommandListener(this);

		try {
			//创建httpconnection 连接
			conn = (HttpConnection) Connector.open(url);
			conn.setRequestMethod(HttpConnection.POST);

			//设置连接属性
			conn.setRequestProperty("User-Agent",
					"Profile/MIDP-2.0 Configuration/CLDC-1.1");
			conn.setRequestProperty("Content-Language", "en-US");
			conn.setRequestProperty("Content-Type", "application/octet-stream");
			conn.setRequestProperty("Accept", "application/octet-stream");
			conn.setRequestProperty("Connection", "Keep-Alive");

			
			if (conn.getResponseCode() == HttpConnection.HTTP_OK) {

				iStrm = conn.openInputStream();
				DataInputStream dis = new DataInputStream(iStrm);

				//获得数据信息
				int length = (int) conn.getLength();
				
				if (length > 0) {
					//读取传输过来的问题的数目
					int num = dis.readInt();
					System.out.println(num);
					
					String question;
					for(int i = 0;i < num;i++){
						//读取问题
						question = dis.readUTF();
						//显示返回信息
						f.append(new StringItem("第 "+ i +" 题: \n" , question));
						f.get(i).setLayout(Item.LAYOUT_EXPAND|Item.LAYOUT_NEWLINE_AFTER);
					}

				} else
					f.append("不能访问数据!");
			}

			

		} catch (Exception e) {
			f.append("网络出错");
			System.out.println(e.toString());
		} finally {
			//关闭连接对象
			if (iStrm != null)
				iStrm.close();
			if (conn != null)
				conn.close();

		}
		display.setCurrent(f);

	}

	public void sendAnswer() throws IOException {
		HttpConnection conn = null;

		OutputStream oStrm = null;

		String url = "http://127.0.0.1:8080/TEst/IQAnswer";

		try {
			//创建httpconnection 连接
			conn = (HttpConnection) Connector.open(url);
			conn.setRequestMethod(HttpConnection.POST);

			//设置连接属性
			conn.setRequestProperty("User-Agent",
					"Profile/MIDP-2.0 Configuration/CLDC-1.1");
			conn.setRequestProperty("Content-Language", "en-US");
			conn.setRequestProperty("Content-Type", "application/octet-stream");
			conn.setRequestProperty("Accept", "application/octet-stream");
			conn.setRequestProperty("Connection", "Keep-Alive");

			Person personIn = new Person(userid, username, "ABCDAB");
			byte[] data = personIn.serialize();
			conn.setRequestProperty("Content-Length", Integer
					.toString(data.length));

			oStrm = conn.openOutputStream();
			oStrm.write(data);
			//提示答案发送成功
			Alert a = new Alert("提示", "发送成功", null, AlertType.INFO);
			a.setTimeout(Alert.FOREVER);
			display.setCurrent(a);

		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			//关闭连接对象
			if (conn != null)
				conn.close();
			if (oStrm != null)
				oStrm.close();

		}
	

	}

	public void commandAction(Command c, Displayable s) {
		if (c == exitCommand) {
			destroyApp(false);
			notifyDestroyed();
		}
		if (c == connectCommand) {
			userid = useridField.getString();
			username = usernameField.getString();
			if (userid.length() == 0 && username.length() == 0) {
				Alert a = new Alert("警告", "姓名和编号不能为空", null, AlertType.ERROR);
				a.setTimeout(Alert.FOREVER);
				display.setCurrent(a);
				return;
			}
			//启动线程
			Thread t = new Thread() {
				public void run() {
					//访问服务器servlet
					try {
						getQuestion();
					} catch (Exception e) {
						System.out.println(e.toString());
					}
				}
			};
			t.start();
		}
		if (c == backCommand) {
			display.setCurrent(mainForm);
		}
		if (c == sendCommand) {
			try {
				//发送答案
				sendAnswer();
				display.setCurrent(mainForm);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -