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

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

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

    隨筆-159  評(píng)論-114  文章-7  trackbacks-0

    我這天,開發(fā)網(wǎng)絡(luò)連接程序,遇到n多問題,都得以解決。

    總結(jié)一下。

    首先是環(huán)境。

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

    現(xiàn)在UltraEdit下面寫一個(gè)網(wǎng)絡(luò)程序。注意一定要在網(wǎng)絡(luò)連接動(dòng)作時(shí),另外啟動(dòng)一下線程,否則模擬器運(yùn)行會(huì)有錯(cuò)誤。

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

    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("服務(wù)器","www.google.com",20,TextField.ANY);
            port 
    = new TextField("端口","80",4,TextField.NUMERIC);
            showBox 
    = new TextBox("連接數(shù)據(jù)""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;
                }

            }

            
            
        }


    }


    這段代碼,在模擬器上運(yùn)行很有問題!

    1.gif

    這以后,再按鈕就沒有反應(yīng)了,而且,會(huì)有警告warning

    警告: 若要避免潛在的死鎖,應(yīng)該在 commandAction() 處理程序之外的其他線程中執(zhí)行
     可能會(huì)阻塞的
     操作(如網(wǎng)絡(luò)連接)。

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

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

    改代碼如下:

    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("請(qǐng)輸入網(wǎng)址","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() ; //連接網(wǎng)址
                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(
    "準(zhǔn)備連接:"+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(
    "內(nèi)容長(zhǎng)度:" + 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("查詢結(jié)果",line.toString(),null,AlertType.CONFIRMATION);
             al.setTimeout(Alert.FOREVER) ;
             display.setCurrent(al) ;
        }

    }



    可以了,在運(yùn)行代碼,就會(huì)看到模擬同樣出現(xiàn)提示,但是確認(rèn)后,模擬器會(huì)走本地的網(wǎng)絡(luò),鏈接站點(diǎn)。

    2.gif

    3.gif

    4.gif

    5.gif

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

    注意,這僅僅是模擬器,部署到手機(jī)上,要對(duì)程序稍加改動(dòng),才能用。

    在中國(guó)移動(dòng)提供的網(wǎng)絡(luò)連接中,分為CMNET和CMWAP兩種,其中CMNET可以無限制的訪問互聯(lián)網(wǎng)絡(luò),資費(fèi)比較貴。CMWAP類似一個(gè)HTTP的代碼,只能訪問支持HTTP的應(yīng)用,但是資費(fèi)便宜,穩(wěn)定性比較差。
      在實(shí)際的J2ME網(wǎng)絡(luò)編程中,一般需要提供以CMWAP代理的方式連接網(wǎng)絡(luò),在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");
      在實(shí)際使用過程中,只需要使用實(shí)際需要訪問的地址的域名或者IP來代替ServerName,例如示例中的“www.test.com”,使用后續(xù)的地址類代替代碼中的url,例如示例中的“l(fā)ogin/loginServlet”,就可以實(shí)際的使用CMWAP代理來進(jìn)行連接了。


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



    主站蜘蛛池模板: 亚洲精品视频久久久| 天天拍拍天天爽免费视频| 亚洲一区二区三区免费| 国产亚洲精品美女| 国产特级淫片免费看| 国产精品无码亚洲一区二区三区| 亚洲免费网站观看视频| 亚洲香蕉久久一区二区| 24小时日本在线www免费的| 亚洲中文字幕久久无码| 日韩免费高清视频| 美女视频黄频a免费| 国产黄色一级毛片亚洲黄片大全| 亚洲免费日韩无码系列| 亚洲精品国产精品乱码在线观看| 美女视频黄的免费视频网页| 久久久久久久综合日本亚洲| 91av免费观看| 亚洲色大成网站www久久九| 国产又长又粗又爽免费视频| 国产99精品一区二区三区免费 | 成年女人18级毛片毛片免费观看| 最近2019中文字幕免费直播| 亚洲人成人77777在线播放| 久久受www免费人成_看片中文| 亚洲乱码中文字幕在线| 丁香亚洲综合五月天婷婷| 免费看黄的成人APP| 久久精品国产99国产精品亚洲| 国产一级淫片免费播放电影| 好男人资源在线WWW免费| 亚洲美女视频网址| 国产又大又粗又硬又长免费 | 亚洲国产精品综合久久一线| 国产免费无码AV片在线观看不卡| 亚洲国产高清视频在线观看| 日韩亚洲精品福利| 最近中文字幕免费mv在线视频| 亚洲精品乱码久久久久久蜜桃图片| 国产成人99久久亚洲综合精品| 91嫩草免费国产永久入口|