j2me 线程

package http;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class HttpServlet2 extends MIDlet implements CommandListener{

    private Display dis;
    private Form frm=new Form("网页信息为");  //记住new
    private TextField tf=new TextField("输入地址","",50,TextField.ANY);
    private Command submit=new Command("登陆",Command.SCREEN,1);
   
    public HttpServlet2() {
        // TODO Auto-generated constructor stub
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        dis=Display.getDisplay(this);
        dis.setCurrent(frm);
        frm.addCommand(submit);
        frm.append(tf);
        frm.setCommandListener(this);


    }

    public void commandAction(Command c, Displayable d) {
        // TODO Auto-generated method stub
        if(c==submit){
//            String url=tf.getString();  //获取登陆的url
//            "http://localhost:8080/lbs/mobile_terminal?"
//            +"TS==1234567891&UserID=10226&StartIndex=1&Count=5"
//            url+="mobile_terminal?TS==1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5";
            try {
                HttpConnection hc=(HttpConnection)Connector.open("http://localhost:8080/lbs/mobile_terminal?TS=1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5");
                System.out.println(hc.getResponseCode());
                DataInputStream di=hc.openDataInputStream();
                int length=(int) hc.getLength();  //读servlet居然是负数
//                byte [] b= new byte[length];
                System.out.println(length);
                byte [] b= new byte[1000];
                di.read(b);
                String str=new String(b);
                System.out.print(str);
                frm.append(str);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        }
       
    }

}

 

 

Warning: To avoid potential deadlock, operations that may block, such as
 networking, should be performed in a different thread than the
 commandAction() handler.

 

以上代码不能执行,可能造成死锁

 

 

解决办法

改用线程

 

 

package http;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
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.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class HttpServlet2 extends MIDlet implements CommandListener{

    private Display dis;
    private Form frm=new Form("网页信息为");  //记住new
    private TextField tf=new TextField("输入地址","",50,TextField.ANY);
    private Command submit=new Command("登陆",Command.SCREEN,1);
   
    public HttpServlet2() {
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        dis=Display.getDisplay(this);
        dis.setCurrent(frm);
        frm.addCommand(submit);
        frm.append(tf);
        frm.setCommandListener(this);


    }

    public void commandAction(Command c, Displayable d) {
        if(c==submit){
        ThreadDemo td=new ThreadDemo();
        td.start();
           
        }
       
    }
   
    class ThreadDemo extends Thread{
        public void run(){
            try {
                HttpConnection hc=(HttpConnection)Connector.open(tf.getString()+"mobile_terminal?TS=1234567891&UserID=10226&Cmd=GetMyGifts&StartIndex=1&Count=5");
                System.out.println(hc.getResponseCode());
                DataInputStream di=hc.openDataInputStream();
                int length=(int) hc.getLength();  //读servlet居然是负数
                System.out.println(length);
                byte [] b= new byte[1000];
                di.read(b);
                String str=new String(b);
                System.out.print(str);
                frm.append(str);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值