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

📄 internetconnection.java

📁 Online Map for Mobile
💻 JAVA
字号:
import java.io.*;
import javax.microedition.io.*;

public class InternetConnection extends Thread 
{ 
    String   url;
    Receiver receiver;

    static final int INIT_BUF_SIZE = 1024;
    static final int BUF_SIZE_EXTENT = 16*1024;
    
    public void run() { 
        HttpConnection c = null;
        InputStream is = null;
        OutputStream os = null;
        
        try {
            try {
                //System.out.println("Connect to " + url);
                c = (HttpConnection)Connector.open(url);
                // Set the request method and headers
                c.setRequestMethod(HttpConnection.GET);
                c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                // c.setRequestProperty("Content-Language", System.getProperty("microedition.locale"));
                
                // Opening the InputStream will open the connection
                // and read the HTTP headers. They are stored until
                // requested.
                is = c.openInputStream();
                
                // Get the ContentType
                String type = c.getType();
                //System.out.println("Connection type=" + type);
                
                // Get the length and process the data
                int len = (int)c.getLength();
                //System.out.println("Content length =" + len);
                
                byte[] body;
                if ("com.mot.cldc.io.j2me.http.Protocol".equals(c.getClass().getName())) { 
                    // Motorola implementation of connection class is something very 
                    // strage having no relation to standard J2ME connections behavoir
                    if (len >= 0) {
                        body = new byte[len+4];
                        int offs = 0;
                        while (offs < len) { 
                            int rc = is.read(body, offs, len - offs);
                            if (rc >= 0) { 
                                offs += rc;
                            } else { 
                                receiver.failure(url);
                                return;
                            }
                        }
                    } else {
                        int rc;
                        body = new byte[INIT_BUF_SIZE];
                        
                        for (len = 0; (rc = is.read(body, len, body.length-len-4)) >= 0;) {
                            if (rc == 1) { 
                                if (body[len] == -1) { 
                                    break;
                                }
                            }
                            len += rc;
                            if (len+4 == body.length) { 
                                int newLen = (len+4)*2 < BUF_SIZE_EXTENT ? (len+4)*2 : len + 4 + BUF_SIZE_EXTENT;
                                byte[] newBody = new byte[newLen];
                                System.arraycopy(body, 0, newBody, 0, len);
                                body = newBody;
                            }
                        }
                    }
                } else { 
                    // normal implementation
                    if (len >= 0) {
                        body = new byte[len];
                        int offs = 0;
                        while (offs < len) { 
                            int rc = is.read(body, offs, len - offs);
                            if (rc >= 0) { 
                                offs += rc;
                            } else { 
                                receiver.failure(url);
                                return;
                            }
                        }
                    } else {
                        int rc;
                        body = new byte[INIT_BUF_SIZE];
                        for (len = 0; (rc = is.read(body, len, body.length-len)) >= 0;) {
                            len += rc;
                            if (len == body.length) {
                                int newLen = len*2 < BUF_SIZE_EXTENT ? len*2 : len + BUF_SIZE_EXTENT;
                                byte[] newBody = new byte[newLen];
                                System.arraycopy(body, 0, newBody, 0, len);
                                body = newBody;
                            }
                        }
                    }
                }
                receiver.received(url, body, len);
            } finally { 
                if (is != null) { 
                    is.close();
                }
                if (os != null) { 
                    os.close();
                }
                if (c != null) { 
                    c.close();
                }
            } 
        } catch (Exception x) { 
            x.printStackTrace();
            System.out.println("Exception " + x);
            receiver.failure(url);
        }
    }

    InternetConnection(String url, Receiver receiver) { 
        this.receiver = receiver;
        this.url = url.startsWith("http://") ? url : "http://" + url;
        start();
    }
}
        

⌨️ 快捷键说明

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