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

📄 connection.java.svn-base

📁 利用J2ME编写的手机应用程序。 功能包括显示图片
💻 SVN-BASE
字号:
package wFramework;
import javax.microedition.io.*;
import javax.microedition.lcdui.Image;

import java.io.IOException;
import java.io.InputStream;

/**
 * Simple threaded class used to connect to HTTP servers.
 * @author Marius Bj鴕ge
 *
 */
public class Connection extends Thread
{
	private ConnectionListener listener;
	private String url;
	private String result;
	private Object param;
	private boolean working;
	private boolean getimage;
	private Image img;
	
	/**
	 * Constructor. Receives a connection listener.
	 * @param listener
	 */
	public Connection(ConnectionListener listener)
	{
		this.listener = listener;
		working = false;
	}
	
	public Connection()
	{
		listener = null;
		working = false;
	}

	/**
	 * Opens URL for asynchronous communication. Result data will be output to listener.
	 * @param url
	 * @param param
	 */
	public void openURLAsync(String url, Object param)
	{
		getimage = false;
		this.url = url;
		this.param = param;
		start();
	}
	
	public void openURLAsync(String url)
	{
		getimage = false;
		openURLAsync(url, null);
	}

	/**
	 * Opens url for synchronous communication. The method will stall until data is read. Use with care..
	 * @param url
	 * @return
	 */
	public String openURLSync(String url)
	{
		this.url = url;
		working = true;
		start();
		
		try 
		{
			while (working)
				sleep(1);
		} 
		catch (InterruptedException e) 
		{
		}
		
		return result;
	}
	
	public void getImage(String url, Object param)
	{
		getimage = true;
		this.url = url;
		this.img = null;
		this.param = param;
		start();
	}
	
    public void run()
    {
    	result = "";
		try
		{
			if (getimage)
				getImageHTTP(url);
			else
			{
				result = getViaHttpConnection(url);
				if (listener != null && !working)
					listener.dataSuccess(result, param);
			}
		}
		catch (IOException e)
		{
			result = e.toString();

			if (listener != null && !working)
				listener.dataFailure(result, param);
		}		
		working = false;
	}
    
    private void getImageHTTP(String url) throws IOException
    {
		HttpConnection c = null;
		InputStream is = null;
		int rc;
		url = fixUrl(url);

		try
		{
			c = (HttpConnection)Connector.open(url);
			c.setRequestProperty("Connection", "close");

			rc = c.getResponseCode();
			if (rc != HttpConnection.HTTP_OK)
				throw new IOException("HTTP response code: " + rc);

			is = c.openInputStream();
			img = Image.createImage(is);
			
			if (listener != null)
				listener.imageSuccess(img, param);
		}
		catch (ClassCastException e)
		{
			throw new IllegalArgumentException("Not an HTTP URL");
		}
		finally
		{
			if (is != null)
				is.close();
			if (c != null)
				c.close();
		}
    }
	
	private String getViaHttpConnection(String url) throws IOException
	{
		HttpConnection c = null;
		InputStream is = null;
		int rc;
		String back = "";
		url = fixUrl(url);

		try
		{
			c = (HttpConnection)Connector.open(url);
//			c.setRequestMethod(HttpConnection.POST);
//			c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			c.setRequestProperty("Connection", "close");

			// Getting the response code will open the connection,
			// send the request, and read the HTTP response headers.
			// The headers are stored until requested.
			rc = c.getResponseCode();
			if (rc != HttpConnection.HTTP_OK)
			{
				throw new IOException("HTTP response code: " + rc);
			}

			is = c.openInputStream();

			// Get the ContentType
//			String type = c.getType();

			// Get the length and process the data
			int len = (int)c.getLength();
			if (len > 0)
			{
				int actual = 0;
				int bytesread = 0 ;
				byte[] data = new byte[len];
				while ((bytesread != len) && (actual != -1))
				{
					actual = is.read(data, bytesread, len - bytesread);
					back += new String(data, 0, actual);
					bytesread += actual;
					
					if (listener != null)
						listener.dataProgress(bytesread, len);
				}
			}
			else // leser ein og ein byte inntil ikkje fleire bytes 

⌨️ 快捷键说明

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