网络多线程编程——01

package com;

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

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class WLcf extends MIDlet implements CommandListener {
	
	private Display disa;
	private Form forma;
	private TextField tfd1,tfd2,tfd3;
	private Command exitCmd,nextCmd,backCmd;
	private Alert al;
	private TextBox tbx;
	String stra;
	private boolean condition=false;
	public WLcf() {
		this.ont();
		al = new Alert("提示信息");
		al.setTimeout(Alert.FOREVER);
		al.setString("移动查询初级版本");
		disa=Display.getDisplay(this);
		
		Gauge gauge = new Gauge(null, false, Gauge.INDEFINITE, Gauge.INCREMENTAL_IDLE);

		al.setIndicator(gauge);
		
		disa=Display.getDisplay(this);
		disa.setCurrent(al,forma);
		
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		

	}

	protected void pauseApp() {
		

	}

	protected void startApp() throws MIDletStateChangeException {
		this.condition=false;
		threadm th=new threadm(this);
		th.start();
	}

	public void commandAction(Command cmd, Displayable dis) {
			if(cmd==exitCmd){
				try {
					destroyApp(false);
					notifyDestroyed();
				} catch (MIDletStateChangeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			if(cmd==nextCmd){
				this.two();
				synchronized(this){
					this.condition=true;
					notify();	
				}
				disa.setCurrent(this.tbx);
			}
			if(cmd==backCmd){
				disa.setCurrent(forma);
			}
	}
	public void ont(){
		forma =new Form("");
		tfd1=new TextField("city", "", 200, 0);
		tfd2=new TextField("start", "", 200, 0);
		tfd3=new TextField("end", "", 200, 0);
		exitCmd=new Command("exit",Command.EXIT,1);
		nextCmd=new Command("next",Command.SCREEN,1);
		forma.addCommand(exitCmd);
		forma.addCommand(nextCmd);
		forma.append(tfd1);
		forma.append(tfd2);
		forma.append(tfd3);
		forma.setCommandListener(this);
	}
	
	public void two(){
		tbx=new TextBox("您的信息", "", 89939, 0);
		backCmd=new Command("back",Command.BACK,1);
		tbx.addCommand(backCmd);
		tbx.setCommandListener(this);                            
	}
	
	public String neirong(){
		String url="http://www.google.com";
		try {
			HttpConnection conn=(HttpConnection)Connector.open(url);
			InputStream istr=(InputStream)conn.openInputStream();
			int ch;
			StringBuffer sbf=new StringBuffer();
			while((ch=istr.read())!=-1){
				sbf.append((char)ch);
				stra=sbf.toString();
			}
			istr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stra;
	}
	class threadm extends Thread{
		MIDlet parents;
		public  threadm(MIDlet parents){
			this.parents=parents;
		}
		public void run(){
			while(true){
				synchronized(parents){
					while(!condition){
						try {
							parents.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					condition=false;
				}
				neirong();//初始化这个方法返回stra的值
				tbx.setString(stra);//给textBox加内容是应该在这里加
			}
		}
	}

}

 

你可能感兴趣的:(多线程,thread,编程,Google)