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

📄 client.java

📁 《Java ME手机应用开发大全》源码 書籍內容簡介: http://www.china-pub.com/410
💻 JAVA
字号:
import java.io.*;
import java.lang.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Client extends MIDlet implements CommandListener 
{
	//声明连接的URL地址 
	private static String url = "http://localhost:8080/ServletContext/servlet/ServerAlias";
	Display display;
	Command exitCommand = new Command( "Exit", Command.EXIT, 1 );
	Command okCommand = new Command( "OK", Command.OK, 1 );
	Command sendCommand = new Command( "Send", Command.OK, 1 );
	private TextBox entryForm;
	public Client(){}
	protected void destroyApp( boolean unconditional ) throws MIDletStateChangeException 
	{
		exitMIDlet();
	}
	protected void pauseApp(){}
	protected void startApp() throws MIDletStateChangeException 
	{
		if( display == null )
		{
			display = Display.getDisplay( this );
			// 声明Form屏幕对象
			Form contain_Form = new Form("Container Form");
			// 声明List对象
			List menu;
			// 声明TextBox对象用来接收用户的输入
			TextBox input;
			display = Display.getDisplay(this);
			menu = new List("SMMB          Account Enquiry", Choice.IMPLICIT);
			menu.append("Current Balance ", null);
			menu.append("Fixed Deposit ", null);
			menu.append("Check Status ", null);
			menu.addCommand(exitCommand);
			menu.addCommand(okCommand);
			menu.setCommandListener(this);
			menu.setCommandListener(this);
			display.setCurrent(menu);
		}
	}
	public void exitMIDlet()
	{
		notifyDestroyed();
	}
	public void commandAction(Command c, Displayable d )
	{
		if( c == sendCommand )
		{
			StatusForm form_Status = new StatusForm(entryForm.getString());
			display.setCurrent( form_Status );
			form_Status.start();
		}
		else if( c == okCommand )
		{
			entryForm = new EntryForm();
			display.setCurrent( entryForm );
		}
	 	else 
		{
			exitMIDlet();
		}
	}
	// 声明TextBox类的子类
	class EntryForm extends TextBox 
	{
		EntryForm()
		{
			super( "Enter your PIN", "", 80, 0 );
			addCommand( exitCommand );
			addCommand( sendCommand );
			setCommandListener( Client.this );
		}
	}
   // 定义一个现实正在连接服务器信息的屏幕 
	class StatusForm extends Form implements Runnable, MyHttpConnection.Callback
	{
		StatusForm( String text )
		{
			super( "Status" );
			// 将字符串转换成字节数组
			try
			{
				ByteArrayOutputStream byte_Out = new ByteArrayOutputStream();
				DataOutputStream data_Out = new DataOutputStream( byte_Out );
				data_Out.writeUTF( text );
				data = byte_Out.toByteArray();
				data_Out.close();
			}
			catch( IOException e )
			{
			}
		}
		//定义显示字符串的方法
		void display( String text )
		{
			if( message == null )
			{
				message = new StringItem( null, text );
				append( message );
			}
			else
			{
				message.setText( text );
			}
		}
		void done( String msg )
		{
			display( msg != null ? msg : "Done." );
			addCommand( okCommand );
			addCommand(exitCommand);
			setCommandListener( Client.this );
		}
	// 根据给定的URL地址,使用HttpConnection 进行HTTP连接
		public void prep_Request(String originalURL, HttpConnection httpConn ) throws IOException
		{
			httpConn.setRequestMethod(HttpConnection.POST );
			httpConn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
			httpConn.setRequestProperty("Content-Language", "en-US" );
			httpConn.setRequestProperty("Accept", "application/octet-stream" );
			httpConn.setRequestProperty("Connection", "close" );
			httpConn.setRequestProperty("Content-Length", Integer.toString( data.length ) );
			OutputStream output_Stream = httpConn.openOutputStream();
			output_Stream.write( data );
			output_Stream.close();
		}
		//为了确保在服务器响应之前程序能够正常执行,必须在单独的线程中打开连接
		public void run()
		{
			HttpConnection httpConn = null;
			display("Obtaining Connection from Server..." );
			try 
			{
			httpConn = MyHttpConnection.connect(url, this );
			display("Connecting to the server..." );
			int response_Code = httpConn.getResponseCode();
			if( response_Code == HttpConnection.HTTP_OK )
			{
			StringBuffer text = new StringBuffer();
			// 读取连接中的数据
				try 
				{
				DataInputStream data_In = new DataInputStream(httpConn.openInputStream() );
				int number = data_In.readInt();
				while( number-- > 0 )
				{
					text.append(data_In.readUTF() );
					text.append( '\n' );
				}
			}
			catch( IOException e )
			{
			}
			done("Your current balance:\n" + text.toString() );
		}
			else
			{
				done("Unexpected return code: " + response_Code );
			}
			}
			catch( IOException e )
			{
				done( "Exception " + e + " trying to connect." );
			}
		}
	//启动线程
		void start()
		{
			display( "Starting to Connect to Server..." );
			Thread thread1 = new Thread( this );
			try 
			{
				thread1.start();
			}
			catch( Exception e )
			{
				done( "Exception " + e + " trying to start thread." );
			}
		}
		private StringItem message;
		private byte[]     data;
	}
}

⌨️ 快捷键说明

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