java.io.IOException:初始化HTTP隧道连接时出错:

时间:2012-03-12 13:54:11

标签: http networking java-me midp

我编写了一个Java Me代码,但是我得到了上面的错误。问题是什么 ?你能解释一下并为我提供解决方案吗? 下面是我编写的一个简单代码。

代码使用POST方法连接到google.com,然后打印字符串缓冲区文件

    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintStream;

    import javax.microedition.io.ConnectionNotFoundException;
    import javax.microedition.io.Connector;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.io.OutputConnection;
    import javax.microedition.lcdui.Alert;
    import javax.microedition.lcdui.AlertType;
    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.midlet.MIDlet;
    import javax.microedition.midlet.MIDletStateChangeException;


    public class NetworkFile extends MIDlet implements CommandListener {
        Form form;
        Display display;
        Command start,exit;

        public NetworkFile() {
            // TODO Auto-generated constructor stub
            form=new Form(null);
            exit=new Command("Exit",Command.EXIT, 0);
            start=new Command("Start",Command.OK, 1);
            form.addCommand(exit);
            form.addCommand(start);
            form.setCommandListener(this);
        }

        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
            display=Display.getDisplay(this);
            display.setCurrent(form);
        }

        public void commandAction(Command c, Displayable d) {
            // TODO Auto-generated method stub

            if (c==start) {

                Abc t=new Abc(this);
                t.start();


            } else if(c==exit){
                notifyDestroyed();
            }


        }

    }


    class Abc extends Thread
    {
        NetworkFile net1;
        HttpConnection hs=null;
        DataOutputStream requestOutputStream;
        InputStream is=null;
        StringBuffer sb=null;
        Abc(NetworkFile net1)
        {
            this.net1=net1;
        }
        public void run()
        {
            try {

                System.out.println("Line 1");
                hs = (HttpConnection)Connector.open("http://www.google.com",Connector.READ_WRITE, true);
                System.out.println("Line 2");
                hs.setRequestMethod(HttpConnection.POST);
                System.out.println("Line 3");
                System.out.println(sb);
                is=hs.openInputStream();
                System.out.println(sb);
                System.out.println("Line 4");
                int ch=0;

                sb = new StringBuffer();
                for (int i = 0; i < 150; i++) {
                    System.out.println("Line 5");
                    ch=is.read();
                    System.out.println("Line 6");
                                if(ch==-1)
                                    {
                                        break;
                                }
                    System.out.println("ch "+ch);
                    sb.append((char)ch);

                }




            } catch (Exception e)
{
System.out.println("Error Message :- " e.getMessage());
}           
System.out.println("sb"+sb.toString());
        }
    }

0 个答案:

没有答案