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

📄 httpconnectiontest.java

📁 网络连接
💻 JAVA
字号:
package com.hu.midlet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class HTTPConnectionTest extends MIDlet implements Runnable {

	private String URL = "http://localhost:8080/jsp-examples/beijing.xml";

	// private String URL = "/text.xml";

	private String text = null;

	static String name = null;

	Vector v = new Vector();

	public HTTPConnectionTest() {

	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		// Thread t = new Thread(this);
		// t.start();
/*
		try { // 删除指定的数据库
			RecordStore.deleteRecordStore("date");
			System.out.println("----deleteRecordStore----");
		} catch (RecordStoreNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecordStoreException e) {
			// TODO Auto-generated catch block e.printStackTrace();
		}
*/
		run();
		v = readRS("date");
		for (int i = 0; i < v.size(); i++) {
			name = (String) v.elementAt(i);
			System.out.println(name);
		}

	}

	// 代码转换
	public static byte[] encode(byte[] name) {
		byte[] result = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(bos);
			//dos.writeUTF(name);
			//result = bos.toByteArray();
			result = name;
			dos.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 代码转换
	public static void decode(byte[] data) {
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			// InputStream in = bis;// 这变量在解析xml文件的时候有用
			DataInputStream dis = new DataInputStream(bis);
			name = dis.readUTF();
			dis.close();
			bis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
		HttpConnection hc = null;// 网络连接
		InputStream in = null;
		try {
			//if (!exit("date")) {
				if (URL.indexOf("http") == 0) {
					hc = (HttpConnection) Connector.open(URL);
					in = hc.openInputStream();
				} else {
					in = getClass().getResourceAsStream(URL);

				}
				// InputStream in = getClass().getResourceAsStream(URL);
				byte[] b = new byte[32];// byte 数据的流量大小
				//
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				DataOutputStream dos = new DataOutputStream(out);
				int i = 0;
				while ((i = in.read(b)) != -1) {
					dos.write(b, 0, i);
					// System.out.println(name);
				}
				dos.flush();
				byte[] result = out.toByteArray();
				dos.close();
				out.close();
				// System.out.println(new String(result,"UTF-8"));
				// String s = new String(result, "UTF-8");
				 writeRS("date", result);// 在数据库中存储文件内容
//			} else {
//				System.out.println("数据库已存在");
//			}

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

	//
	public static RecordStore openRsAnyway(String table) {
		RecordStore rs = null;
		if (table.length() > 32)
			return null;
		try {
			rs = RecordStore.openRecordStore(table, true);
			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 在管理记录存储中创建表
	public boolean writeRS(String tableName, byte[] name) {
		RecordStore rs = openRsAnyway(tableName);
		if (rs == null) {
			System.out.println("table open fail");
		} else {
			try {
				byte tmp[] = encode(name);
				rs.addRecord(tmp, 0, tmp.length);
				// System.out.println(rs.getName());
				// rs.addRecordListener(null);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			} finally {
				if (rs != null) {
					try {
						rs.closeRecordStore();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return true;
	}

	// 送管理记录存储中读取记录
	public static Vector readRS(String tableName) {
		RecordStore rs = openRsAnyway(tableName);
		Vector vector = new Vector();
		if (rs == null) {
			System.out.println("table open fail");
		} else {
			try {
				int lastID = rs.getNextRecordID();
				for (int i = 1; i < lastID; i++) {
					int size = rs.getRecordSize(i);
					byte data[] = new byte[size];
					data = rs.getRecord(i);
					// System.out.println(+lastID);
					decode(data);
					// System.out.println(name);
					vector.addElement(name);
				}

			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (rs != null) {
					try {
						rs.closeRecordStore();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return vector;
	}

	//
	public static boolean exit(String tableName) {
		RecordStore rs = null;
		try {
			System.out.println("ok");
			rs = RecordStore.openRecordStore(tableName, false);
			return true;

		} catch (Exception e) {
			// e.printStackTrace();
			return false;
		} finally {
			if (rs != null) {
				try {
					rs.closeRecordStore();
				} catch (Exception e) {
					// e.printStackTrace();
				}
			}
		}

	}
}

⌨️ 快捷键说明

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