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

📄 iotask.java

📁 j2me mobile application for upload file to server.
💻 JAVA
字号:
/**
 * 
 */
package org.celllife.clforms.task;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;

import org.celllife.clforms.Constants;
import org.celllife.clforms.XFormScreen;
import org.celllife.clforms.api.XForm;
import org.celllife.clforms.xml.XMLUtil;

import de.enough.polish.util.Locale;

/**
 * @author Simon
 * 
 */
public class IOTask extends BackgroundTask {

	private String url;

	private boolean read;

	private XForm xf;

	/**
	 * @param d
	 */
	public IOTask(String url, Display d, boolean read) {
		super(d);
		// TODO Auto-generated constructor stub
		this.url = url;
		this.read = read;
		this.title = "";
		prevScreen = display.getCurrent();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.celllife.clforms.thread.BackgroundTask#runTask()
	 */
	public void runTask() throws Exception {
		if (read)
			read();
		else
			write();
	}

	/**
	 * 
	 */
	private void write() {
		xf.populateModel();
		try {

			FileConnection fc = (FileConnection) Connector.open(url);
			if (!fc.exists()) {
				fc.create();
			}
			fc.close();

			// #debug debug
			XMLUtil.printModel(xf.getXmlModel());

			XMLUtil.writeXMLToFile(url, xf.getXmlModel());

			String[] params = new String[1];
			params[0] = url;
			String message = Locale.get("save.success.message", params);
			needAlert = true;
			alertScreen = new Alert(Locale.get("save.success"));
			alertScreen.setType(AlertType.INFO);
			alertScreen.setString(message);
			alertScreen.setTimeout(Alert.FOREVER);
			nextScreen = prevScreen;

		} catch (IOException e) {
			String s = "Can not access file '" + url + "'";

			if ((e.getMessage() != null) && (e.getMessage().length() > 0)) {
				s += ("\n" + e);
			}

			needAlert = true;
			alertScreen = new Alert(Locale.get("error"));
			alertScreen.setType(AlertType.ERROR);
			alertScreen.setString(s);
			alertScreen.setTimeout(Alert.FOREVER);
			nextScreen = prevScreen;
		}

	}

	/**
	 * @throws Exception
	 */
	private void read() throws Exception {
		String xmlStr = "";
		XForm xf;
		try {
			switch (Constants.MODE) {
			case Constants.OFFLINE:
				xmlStr = XMLUtil.readTextFile(url);
				break;
			case Constants.ONLINE:
				xmlStr = XMLUtil.sendHttpGet(url);
				break;
			}

			if (xmlStr.endsWith("ERROR"))
				throw new Exception("Error reading from server: " + url);
			
			ByteArrayInputStream bais = new ByteArrayInputStream(xmlStr
					.getBytes());
			InputStreamReader isr = new InputStreamReader(bais, "UTF-8");
			xf = XMLUtil.parseForm(isr);
			isr.close();

			xf.setUrl(url);

			//#debug debug
			System.out.println("XF url: " + url);

			nextScreen = (new XFormScreen(xf)).prepareScreen();
		} catch (Exception e) {
			e.printStackTrace();
			needAlert = true;
			alertScreen = new Alert(Locale.get("error")); //$NON-NLS-1$
			alertScreen.setType(AlertType.ERROR);
			alertScreen.setString(e.getMessage());
			alertScreen.setTimeout(Alert.FOREVER);
			nextScreen = prevScreen;
		}
	}

	/**
	 * @param xf
	 */
	public void setXform(XForm xf) {
		this.xf = xf;

	}

}

⌨️ 快捷键说明

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