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

📄 xmlconnection.java.svn-base

📁 利用J2ME编写的手机应用程序。 功能包括显示图片
💻 SVN-BASE
字号:
package wFramework;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.Image;

import java.io.IOException;
import java.io.InputStream;
import org.kxml2.io.*;

/**
 * Simple threaded XML based connection class. 
 * @author Marius Bj鴕ge
 *
 */
public class XMLConnection extends Thread
{	
	private KXmlParser parser;
	private XMLConnectionListener listener;
	private String url;
	private String xmlcmd;
	private Object param;
	private boolean working, getimage;
	private Image image;
	public static int numPending = 0;

	public XMLConnection(XMLConnectionListener listener)
	{
		image = null;
		param = null;
		url = "";
		getimage = false;
		parser = new KXmlParser();
		working = false;
		this.listener = listener;
	}
	
	public XMLConnection()
	{
		image = null;
		working = false;
		getimage = false;
		param = null;
		url = "";
		parser = new KXmlParser();
		listener = null;
	}

	/**
	 * Opens URL for asynchronous communication.
	 * Data will be returned to listener when ready.
	 * @param url
	 * @param xml
	 * @param param
	 */
	public void openURLAsync(String url, String xml, Object param)
	{
		getimage = false;
		this.param = param;
		this.xmlcmd = "xml=" + xml;
		this.url = url;
		start();		
	}
	
	/**
	 * Opens URL for asynchronous communication.
	 * Data will be returned to listener when ready.
	 * @param url
	 * @param xml
	 */
	public void openURLAsync(String url, String xml)
	{
		getimage = false;
		this.xmlcmd = "xml=" + xml;
		openURLAsync(url, xml, null);
	}

	/**
	 * 
	 * @param url
	 * @param xml
	 * @return
	 */
	public KXmlParser openURLSync(String url, String xml)
	{
		getimage = false;
		this.xmlcmd = "xml=" + xml;
		this.url = url;//fixUrl(url + xml);
		working = true;
		start();
		
		try 
		{
			while (working)
				sleep(1);
		} 
		catch (InterruptedException e) 
		{
		}		
		return parser;
	}
	
	/**
	 * Gets image and returns to listener
	 * @param url
	 * @param xml
	 * @param param
	 */
	public void getImage(String url, String xml, Object param)
	{
		this.url = url;
		this.xmlcmd = "xml=" + xml;
		this.param = param;
		this.image = null;
		getimage = true;
		start();
	}
	
	/**
	 * thread run
	 */
	public void run()
    {
    	HttpConnection hc = null;
    	InputStream is = null;

		try
		{
			numPending++;
			hc = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true);
			hc.setRequestMethod(HttpConnection.POST);
			hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			OutputStream os = hc.openOutputStream();
			os.write(xmlcmd.getBytes());
			hc.getResponseCode();
			os.close();

			is = hc.openInputStream();
			if (getimage)
			{
				image = Image.createImage(is);
				numPending--;
				if (listener != null)
					listener.xmlImageSuccess(image, this.param);
			}
			else
			{
				parse(is);
				numPending--;
			}
		}
		catch (Exception e)
		{
			if (listener != null && !working)
				listener.xmlFailure(e.toString(), param);
		}
		finally
		{
			try
			{
				if (hc != null)
					hc.close();
			}
			catch (IOException e) {}
		}
		working = false;
	}

	/**
	 * Parses the incoming data and sends it to the listener
	 * @param in
	 * @throws IOException
	 */
    public void parse(InputStream in) throws IOException
    {
    	Reader reader = new InputStreamReader(in);
    	parser.setInput(reader);
    	
    	try
    	{
	    	if (listener != null && !working)
	    	{
	    		if (QueryListener.class.isInstance(listener))
	    		{
	    			((QueryListener)listener).querySuccess(new ResultSet(parser, null));
	    		}
	    		else
	    			listener.xmlSuccess(parser, param);
	    	}
    	}
    	catch (Exception e)
    	{
    	}
    }

    public static String urlEncode(String in)
    {
    	String out = "";
    	for (int i = 0; i < in.length(); i++)
    	{
    		if (in.charAt(i) == ' ')
    			out += "+";
    		else if (in.charAt(i) == '\'')
    			out += "%22";
    		else if (in.charAt(i) < 65 || in.charAt(i) > 122)
    			out += "%" + Integer.toHexString(in.charAt(i)).toUpperCase();
    		else
    			out += String.valueOf(in.charAt(i));
    	}
    	
    	return out;
    }
    
    /**
     * URL encodes the string
     * @param in
     * @return
     */    
	private String fixUrl(String in)
	{
		String out = "";
		for (int i = 0; i < in.length(); i++)
		{
			if (in.charAt(i) == ' ')
				out += "%20";
			else
				out += in.charAt(i);
		}
		return out;
	}
}

⌨️ 快捷键说明

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