新手刚学,J2ME联网问题
不废话直接上代码:
		//按下左键时登陆,按下右键时离开
		if(keyCode == KEY_SOFT_L&&!loading){
			if("".equals(corpname)||corpname==null){
				currentField = 0;
			}else if("".equals(userno)||userno==null){
				currentField = 1;
			}else if("".equals(userpassword)||userpassword==null){
				currentField = 2;
			}else{
				loginClient.getUservo().setCorp(corpid);
				loginClient.getUservo().setUserno(userno);
				loginClient.getUservo().setUserpassword(userpassword);
				try{
					Thread t = new LoginThread(loginClient);//创建LoginThread线程
					t.start();//线程LoginThread启动为可运行状态,进入LoginThread线程的run方法
					t.join();					
				}catch(Exception e){
					System.out.println(e);
				}
			}
		}
public class LoginThread extends Thread{
	private LoginClient midlet;
	public LoginThread(LoginClient midlet){
		this.midlet = midlet;
	}
	public void run(){
			System.out.println("connecting to server......"+midlet.getUservo().getUserno());
			HttpConnection httpConn = null;
			DataOutputStream dos = null;
			DataInputStream dis = null;
			try{
				httpConn = (HttpConnection)Connector.open("http://192.168.1.100/portal/c/nc/bs/mobile/action/MobileAction?action=login",Connector.READ_WRITE,true);
				httpConn.setRequestMethod(HttpConnection.POST);
				dos = new DataOutputStream(httpConn.openOutputStream());
				dos.writeUTF(midlet.getUservo().getCorp());
				dos.writeUTF(midlet.getUservo().getUserno());
				dos.writeUTF(midlet.getUservo().getUserpassword());
				//连接成功
				if(httpConn.getResponseCode() == HttpConnection.HTTP_OK){
					dis = new DataInputStream(httpConn.openInputStream());
					int length = (int)httpConn.getLength();
					if(length > 0){
						byte[] servletData = new byte[length];
						dis.read(servletData);
						//显示返回消息
						String2byte betyToStr = new String2byte();
						String content = betyToStr.BytesToString(servletData);
						String userno = content.substring(content.indexOf("userno:")+"userno:".length(),  
								content.indexOf(";username:"));
						String username = content.substring(content.indexOf("username:")+"username:".length(),  
								content.indexOf(";userpassword:"));
						String userpassword = content.substring(content.indexOf("userpassword:")+"userpassword:".length(),  
								content.indexOf(";userid:"));
						String userid = content.substring(content.indexOf("userid:")+"userid:".length(),  
								content.indexOf(";corp:"));
						String corp = content.substring(content.indexOf("corp:")+"corp:".length(),  
								content.length()-1);						
						midlet.getUservo().setCorp(corp);
						midlet.getUservo().setUserid(userid);
						midlet.getUservo().setUsername(username);
						midlet.getUservo().setUserno(userno);
						midlet.getUservo().setUserpassword(userpassword);
					}
				}
			}catch(
IOException e){
				System.out.println("==+"+e);
			}
			try{
				if(dos != null){
					dos.close();
					dos = null;
				}
				if(dis != null){
					dis.close();
					dis = null;
				}
				if(httpConn != null){
					httpConn.close();
					httpConn = null;
				}
			}catch(IOException e2){}
				}
}
我的想法是这样的,用canvas做一个手机登陆界面,然后当用户输入账号、密码、单位后,然后新开一个线程,(这个时候主线程暂停)这个线程就是做联网操作,与后台服务器连接,然后把用户输入的账号密码单位传到后来,由后台验证完毕再返回数据,然后这个线程接收数据之后。再把数据存到持久类UserVO中,然后线程结束,然后主线程那边启动,验证返回的数据,然后判断是否做登陆成功操作还是登陆失败的操作。
我上面的代码现在问题在于如果我把代码:t.join();这个注销,那么新开的线程能正常运行也能访问后台数据并且返回数据等等,但是主线程却不会暂停先让新开的线程运行完再运行,然后如果我把t.join();这个加上。却会出现新开的线程一直运行到httpConn = (HttpConnection)Connector.open("http://192.168.1.100/portal/c/nc/bs/mobile/action/MobileAction?action=login",Connector.READ_WRITE,true);这句代码就卡住不动。也没报错信息。请各位大大知道是什么问题么?(我也试过在住线程那边使用循环然后判断新开线程某个对象的值来作是否退出循环这样来让主线程等待新开的线程,但这样做新开的线程还是只会运行到httpConn = (HttpConnection)Connector.open("http://192.168.1.100/portal/c/nc/bs/mobile/action/MobileAction?action=login",Connector.READ_WRITE,true);这句就开住不动了..)