<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-159  評論-114  文章-7  trackbacks-0

    我這天,開發網絡連接程序,遇到n多問題,都得以解決。

    總結一下。

    首先是環境。

    我用的開發SDK和模擬器都是Sun的,J2ME Wireless Toolkit 2.2

    現在UltraEdit下面寫一個網絡程序。注意一定要在網絡連接動作時,另外啟動一下線程,否則模擬器運行會有錯誤。

    很不好的代碼:不要使用?。。。。。。。。?!

    import java.io.InputStream;
    import java.io.OutputStream;

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

    public class MainMidlet extends MIDlet 
    implements CommandListener
    {
        
        
    private Display display;
        
        
    public MainMidlet() {
            
    super();
            display 
    = Display.getDisplay(this);
        }

        
        
    private Form f;
        
        
    private TextField server;
        
        
    private TextField port;
        
        
    private TextBox showBox;
        
        
        
    protected void startApp() throws MIDletStateChangeException {
            f 
    = new Form("Test Network");
            server 
    = new TextField("服務器","www.google.com",20,TextField.ANY);
            port 
    = new TextField("端口","80",4,TextField.NUMERIC);
            showBox 
    = new TextBox("連接數據""Init",1024,TextField.ANY);
            showBox.addCommand(
    new Command("返回",Command.BACK,1));
            showBox.setCommandListener(
    this);
            f.append(server);
            f.append(port);
            f.addCommand(
    new Command("Socket連接",Command.SCREEN,1));
            f.addCommand(
    new Command("Http連接",Command.SCREEN,1));
            f.setCommandListener(
    this);
            display.setCurrent(f);
        }


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

        }


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

        }


        
    public void commandAction(Command arg0, Displayable arg1) {
            
    if(arg0.getLabel().equals("Socket連接"))
            
    {
                socketconnect();
            }

            
    else if(arg0.getLabel().equals("Http連接"))
            
    {
                httpconnect();
            }

            
    else if(arg0.getLabel().equals("返回"))
            
    {
                display.setCurrent(f);
            }

        }

        
        
    private void socketconnect()
        
    {
            StreamConnection socket 
    = null;
            InputStream is 
    = null;
            OutputStream os 
    = null;
            
    try{
                String name 
    = "http://" + server.getString() + ":" + port.getString();
                socket 
    = (StreamConnection)Connector.open(name,Connector.READ_WRITE);
                String request 
    = "GET / HTTP/1.0\n\n";
                os 
    = socket.openOutputStream();
                os.write(request.getBytes());
                
                is 
    = socket.openInputStream();
                
    final int MAX_LENGTH = 128;
                
    byte[] buf = new byte[MAX_LENGTH];
                
    int total = 0;
                
    while(total < MAX_LENGTH){
                    
    int count = is.read(buf,total, MAX_LENGTH - total);
                    
    if(count < 0)
                    
    {
                        
    break;
                    }

                    total 
    += count;
                }

                String toshow 
    = new String(buf,0,buf.length);
                showBox.setString(toshow);
                display.setCurrent(showBox);
            }
    catch(Exception ex)
            
    {
                Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                al.setTimeout(Alert.FOREVER);
                display.setCurrent(al,f);
            }
    finally{
                
    if(is != null)
                
    {
                    
    try{
                        is.close();
                    }
    catch(Exception ex)
                    
    {
                        Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,f);
                    }

                    is 
    = null;
                }

                
    if(os != null)
                
    {
                    
    try{
                        os.close();
                    }
    catch(Exception ex)
                    
    {
                        Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,f);
                    }
                        
                    os 
    = null;
                }

                
    if(socket != null)
                
    {
                    
    try{
                        socket.close();
                    }
    catch(Exception ex)
                    
    {
                        Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,f);
                    }
                        
                    socket 
    = null;
                }

            }

        }

        
        
    private void httpconnect()
        
    {
            HttpConnection conn 
    = null;
            InputStream is 
    = null;
            OutputStream os 
    = null;
            
    try{
                String url 
    = "wap.winwap.com";
                conn 
    = (HttpConnection)Connector.open("http://10.0.0.172/" + "home.wml");
                conn.setRequestProperty(
    "X-Online-Host",url);
                conn.setRequestMethod(HttpConnection.GET);
                
    if(conn.getResponseCode() == HttpConnection.HTTP_OK)
                
    {
                    is 
    = conn.openInputStream();
                    
    final int MAX_LENTH = 128;
                    
    byte[] buf = new byte[MAX_LENTH];
                    
    int total = 0;
                    
    while(total < MAX_LENTH)
                    
    {
                        
    int count = is.read(buf,total,MAX_LENTH - total);
                        
    if(count < 0)
                            
    break;
                        total 
    += count;
                    }

                    is.close();
                    String reply 
    = new String(buf,0,total);
                    showBox.setString(reply);
                    display.setCurrent(showBox);
                }
    else{
                    showBox.insert(String.valueOf(conn.getResponseCode()),showBox.getCaretPosition());
                    showBox.insert(String.valueOf(HttpConnection.HTTP_OK),showBox.getCaretPosition());
                    display.setCurrent(showBox);
                }

            }
    catch(Exception ex)
            
    {
                Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                al.setTimeout(Alert.FOREVER);
                display.setCurrent(al,showBox);
            }
    finally{
                
    if(is != null)
                
    {
                    
    try{
                        is.close();
                    }
    catch(Exception ex)
                    
    {
                        Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,showBox);
                    }

                    is 
    = null;
                }

                
    if(os != null)
                
    {
                    
    try{
                        os.close();
                    }
    catch(Exception ex)
                    
    {
                        Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,showBox);
                    }
                        
                    os 
    = null;
                }

                
    if(conn != null)
                
    {
                    
    try{
                        conn.close();
                    }
    catch(Exception ex)
                    
    {
                        Alert al 
    = new Alert("未定義異常", ex.getMessage(), null, AlertType.ALARM);
                        al.setTimeout(Alert.FOREVER);
                        display.setCurrent(al,showBox);
                    }
                        
                    conn 
    = null;
                }

            }

            
            
        }


    }


    這段代碼,在模擬器上運行很有問題!

    1.gif

    這以后,再按鈕就沒有反應了,而且,會有警告warning

    警告: 若要避免潛在的死鎖,應該在 commandAction() 處理程序之外的其他線程中執行
     可能會阻塞的
     操作(如網絡連接)。

    模擬器再不能正常鏈接了。無法配置。

    ===============================================

    改代碼如下:

    import javax.microedition.io.*;
    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    public class QueryForm extends TextBox implements CommandListener
    {
        MIDlet midlet ;
        
    public QueryForm(MIDlet m)
        
    {
            
    super("請輸入網址","http://wap.winwap.com",40,TextField.ANY) ;
            midlet 
    = m ;
            addCommand(
    new Command("離開",Command.SCREEN,1)) ;
            addCommand(
    new Command("查詢",Command.SCREEN,1)) ;
            setCommandListener(
    this) ;
        }

        
    public void commandAction(Command c,Displayable s)
        
    {
            String cmd 
    = c.getLabel() ;
            
    if(cmd.equals("離開"))
            
    {
                midlet.notifyDestroyed() ;
            }
    else if(cmd.equals("查詢"))
            
    {
                String url 
    = getString() ; //連接網址
                WorkThread wt = new WorkThread(url,Display.getDisplay(midlet)) ;
                wt.start() ;
            }

        }

    }

    import javax.microedition.io.*;
    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    public class MyHttpClient extends MIDlet
    {
        Display display ;
        
    public MyHttpClient()
        
    {
            display 
    = Display.getDisplay(this) ;
        }

        
    public void startApp()
        
    {
            display.setCurrent(
    new QueryForm(this));
        }

        
    public void pauseApp()
        
    {
        }

        
    public void destroyApp(boolean con)
        
    {
        }

    }

    import javax.microedition.io.*;
    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    public class WorkThread extends Thread
    {    
        String url ;
        Display display ;
        
    public WorkThread(String url,Display d)
        
    {
            
    this.url = url ;
            display 
    = d ;
            System.out.println(
    "準備連接:"+this.url) ;
        }

        
    public void run()
        
    {
            HttpConnection conn 
    = null;
            InputStream is 
    = null;
            InputStreamReader isr 
    = null ;
            StringBuffer line 
    = new StringBuffer("");
            
    try {
                    conn 
    = (HttpConnection)Connector.open(url);
                System.out.println(
    "內容長度:" + conn.getLength()) ;
                   is 
    = conn.openInputStream();    
                   isr 
    = new InputStreamReader(is) ;
                   
    int ic ;            
                   
    while( (ic = isr.read()) != -1 )
                
    {
                    line.append((
    char)ic) ;
                }
                
             }
    catch (Exception ioe) 
             
    {
                 System.out.println(ioe);
             }
    finally
             
    {
                 
    try
                 
    {
                     
    if(conn!=null)
                         conn.close();
                 }
    catch(Exception e){}
             }

             Alert al 
    = new Alert("查詢結果",line.toString(),null,AlertType.CONFIRMATION);
             al.setTimeout(Alert.FOREVER) ;
             display.setCurrent(al) ;
        }

    }



    可以了,在運行代碼,就會看到模擬同樣出現提示,但是確認后,模擬器會走本地的網絡,鏈接站點。

    2.gif

    3.gif

    4.gif

    5.gif

    OK,成功在模擬器上訪問了。

    注意,這僅僅是模擬器,部署到手機上,要對程序稍加改動,才能用。

    在中國移動提供的網絡連接中,分為CMNET和CMWAP兩種,其中CMNET可以無限制的訪問互聯網絡,資費比較貴。CMWAP類似一個HTTP的代碼,只能訪問支持HTTP的應用,但是資費便宜,穩定性比較差。
      在實際的J2ME網絡編程中,一般需要提供以CMWAP代理的方式連接網絡,在J2ME中,連接的代碼和直接連接有所不同,代碼如下:
                 HttpConnection http = (HttpConnection)Connector.open(("http://10.0.0.172/"+url);
                 http.setRequestProperty("X-Online-Host",ServerName);
      例如你需要訪問的地址為:http://www.test.com/login/loginServlet則上面的代碼就為:
                 HttpConnection http = (HttpConnection)Connector.open(("http://10.0.0.172/" + "login/loginServlet");
                 http.setRequestProperty("X-Online-Host","www.test.com");
      在實際使用過程中,只需要使用實際需要訪問的地址的域名或者IP來代替ServerName,例如示例中的“www.test.com”,使用后續的地址類代替代碼中的url,例如示例中的“login/loginServlet”,就可以實際的使用CMWAP代理來進行連接了。


    嗚呼,休息一下,在達內學習完C++,最強大的多范型語言,學習J2ME只需要一周就足夠了。好比是九陽神功在體內,無所不能。阿,哈哈哈。Java乃是乾坤大挪移。



    posted on 2006-02-05 13:28 北國狼人的BloG 閱讀(4980) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
    主站蜘蛛池模板: 大学生一级特黄的免费大片视频| 久久精品国产亚洲av日韩| 日韩视频在线精品视频免费观看| 日韩大片免费观看视频播放| 亚洲依依成人精品| 情人伊人久久综合亚洲| 亚洲国产人成精品| 日本久久久免费高清| 99久久精品日本一区二区免费 | 在线观看免费亚洲| 中文字幕av无码无卡免费| 99在线视频免费| a国产成人免费视频| 一级毛片aa高清免费观看| 在线91精品亚洲网站精品成人| 亚洲中文无码永久免| 久久亚洲最大成人网4438| 777亚洲精品乱码久久久久久| 久久精品亚洲中文字幕无码网站| 不卡一卡二卡三亚洲| 国产成人亚洲综合| 亚洲日本韩国在线| 亚洲精品tv久久久久久久久久| 国产精品免费看香蕉| 国产成人无码a区在线观看视频免费| 无码日韩精品一区二区免费| 91精品国产免费久久久久久青草| 午夜视频免费在线观看| 97视频免费观看2区| 午夜精品免费在线观看| 99久久综合精品免费| 91福利免费体验区观看区| 久久精品无码专区免费青青| 8x成人永久免费视频| 国产精品免费网站| 久久久久免费看黄A片APP| 好爽…又高潮了免费毛片| 暖暖在线日本免费中文| 国产成人免费一区二区三区| 免费成人黄色大片| 亚洲韩国精品无码一区二区三区|