⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 networkdetector.java

📁 J2ME自动获取网络连接的方法
💻 JAVA
字号:
package net.sourceforge.j2meautonetwork.operation;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.sourceforge.j2meautonetwork.util.CodeDefine;
import net.sourceforge.j2meautonetwork.util.Utilities;

/**
 * 网络探测器.
 * @author Steven King(woyaoying@hotmail.com)
 * @version 0.3
 */
public final class NetworkDetector implements Runnable {

    private String proxy;

    private boolean connected;
    
    private DataInputStream httpdis = null;
    private DataOutputStream httpdos = null;
    private HttpConnection httpconn = null;
    
    private String errorMsg;

    private boolean runningOver = false;
    
  
    /**
     * 尝试连接网络.
     * @param originalUrl
     * @throws Throwable
     */
    private void tryConnect(String originalUrl) throws Throwable {
        String hostName, servlet, url;
        if (originalUrl.toLowerCase().indexOf("http://") < 0) {
            originalUrl = "http://" + originalUrl;
        }
        int pos1 = "http://".length();
        int pos2 = originalUrl.indexOf('/', pos1);
        if (pos2 < 0) {
            hostName = originalUrl.substring(pos1);
            servlet = "";
        } else {
            hostName = originalUrl.substring(pos1, pos2);
            servlet = originalUrl.substring(pos2);
        }

        int pos3 = hostName.indexOf(":");

        if (pos3 < 0) {
            hostName += ":80";
        }

        if (proxy != null) {
            url = proxy + servlet;
        } else {
            url = "http://" + hostName + servlet;
        }
        httpconn = (HttpConnection) Connector.open(url);
        if (proxy != null) {
            httpconn.setRequestProperty(CodeDefine.HTTP_PROXY_HOST_HEADER,
                    hostName);
            httpconn.setRequestProperty(CodeDefine.HTTP_ACCEPT[0],
                    CodeDefine.HTTP_ACCEPT[1]);
            httpconn.setRequestProperty(CodeDefine.HTTP_USER_AGENT[0],
                    CodeDefine.HTTP_USER_AGENT[1]);
        }
        debug("URL: " + url);
        try {
            if (httpconn != null) {
                httpdis = httpconn.openDataInputStream();
                httpdos = httpconn.openDataOutputStream();
            } else {
                throw new Exception("HttpConnection 不能为空");
            }
        } finally {
            closeConnection();
        }
    }
    
    private void closeConnection(){
        long time = System.currentTimeMillis();
        if (httpdis != null) {
            try {
                httpdis.close();
                httpdis = null;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        debug("close httpdis end " + (System.currentTimeMillis() - time));
        time = System.currentTimeMillis();
        if (httpdos != null) {
            try {
                httpdos.close();
                httpdos = null;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        debug("close httpdos end " + (System.currentTimeMillis() - time));
        time = System.currentTimeMillis();
        if (httpconn != null) {
            try {
//                httpconn.close(); Dont user close, it will be block so long time.
                httpconn = null;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        debug("close httpconn end " + (System.currentTimeMillis() - time));
    }

    private synchronized void setConnected() {
        runningOver = true;
        connected = true;
        debug("Connected true");
    }

    public synchronized void notifyStop() {
        connected = false;
        runningOver = true;
        debug("超时!!!!");
        long time = System.currentTimeMillis();
        closeConnection();
        
        debug("Close connection " + (System.currentTimeMillis() - time));
        //setOver(CodeDefine.NETWORK_ERRORS[1]);
    }

    private void debug(String log) {
        Utilities.debug("Detector "+ proxy + " " + log);
    }
    
    public void run() {
        //timer.schedule(new TimeOutMonitor(this), CodeDefine.HTTP_CONNECT_TIME_OUT);
        try {
            tryConnect("http://wap.baidu.com:80");
            //timer.cancel();
            setConnected();
        } catch (Throwable e) {
            if (e instanceof SecurityException) {
                debug("请同意连接网络,重新启动应用.");
            }
            Utilities.debug("catch try except " + e.getMessage());
            //timer.cancel();
        } finally{
            closeConnection();
        }
    }

    public String getProxy() {
        return proxy;
    }

    public synchronized void setOver() {
        runningOver = true;
    }

    public NetworkDetector(String proxy) {
        this.proxy = proxy;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public synchronized boolean getConnected() {
        return connected;
    }

    public boolean getOver() {
        return runningOver;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -