当前位置: 首页 > 工具软件 > Comtor J2ME > 使用案例 >

j2me 下载

闻人鸿文
2023-12-01

最近老总叫写一个能测试出那些手机在在线播放我们音乐时候会卡的小软件,没写之前感觉很简单,着手一写还是遇到一写问题,如:在有cmwap下载流 文件的时候一般会有一个提示,但是这个提示在有的手机上没有显示出来,从而导致下载的数据不是真正的音乐文件,从而导致下载的数据不对,以前没有类似的经 验,网上也没有类似的资料,弄了半天-----青年节的半天的假也差点达进去。下面说说我的感受吧。
1,弄清cmnect和cmwap连接的区别。
2,再次感觉到书到用时方恨少,大学学的网络只是在这里可以派上用场了,记得当时学的时候就知道"第一次握手,第二次握手"等之类简单的问题。
3,get和post的区别。
下面的代码是关于下载线程的简单列子,重点在判断下载下来的数据是否是真正的音乐文件,很简单----直接判断下载下来的数据的开始是否是"<?xml"开头。

class DownLoadMusic extends Thread {

        private HttpConnection conn = null;

        /**
         *用于判断连网返回数据是否有效
         */
        private final int[] STARTMARK = {0x3c, 0x3f, 0x78, 0x6d, 0x6c}; //<?xml

        /**
         *是否需要重新请求连网表示
         */
        boolean bReqeat = true;

        InputStream is = null;
        private boolean repeateRequest() {
            boolean result = false;
            while (bReqeat) {
                try {
                    if (getConnectMethod() == 0) {
                        System.out.println("cmwap");
                        String realUrl = cmnwap + suffixSeverIp;
                        realUrl = realUrl.trim();
                        conn = (HttpConnection) Connector.open(realUrl);
                        conn.setRequestProperty("X-Online-Host", prefixSeverIp);
                    } else if (getConnectMethod() == 1) {
                        System.out.println("cmnet");
                        String cmnectUrl = urlInputField.getString();
                        conn = (HttpConnection) Connector.open(cmnectUrl);
                        conn.setRequestProperty("Host", suffixSeverIp);
                    }
                    is = conn.openInputStream();
                    if (is != null) {
                        bReqeat = false;
                        result = true;
                    }
                } catch (Exception e) {
                    DownLoadMIDlet.quitApp();
                    e.printStackTrace();
                }
            }
            return result;
        }

        public void run() {
            try {
                startTime = System.currentTimeMillis();
                repeateRequest();
                userCache = getUserCache();
                byte[] buffer = new byte[userCache];
                int recevie_length = 0;
                int index = 0;
                do {
                    recevie_length = is.read(buffer, 0, userCache);
                    boolean b = true;
                    for (int i = 0; i < 5; i++) {
                        b &= (buffer[i] == STARTMARK [i]);
                    }
                    if (b) {
                        bReqeat = true;
                        repeateRequest();
                        recevie_length = 0;
                        index = 0;
                        musicSize = 0;
                        count = 0;
                        continue;
                    }
                    buffer = new byte[userCache];
                    index += recevie_length;
                    if (recevie_length > 0) {
                        musicSize += recevie_length;
                    }
                    if (index >= userCache) {
                        count++;
                        index = 0;
                        ls.repaint();
                    }
                } while (recevie_length != -1);
                endTime = System.currentTimeMillis();
                userCache /= UNIT_BYTE;
            } catch (IOException ieo) {
                DownLoadMIDlet.getInstance().quitApp();
                ieo.printStackTrace();
            }
            long second = (endTime - startTime) / 1000;
            long currentSpeed = (musicSize / UNIT_BYTE) / second;
            if (currentSpeed > bestSpeed) {
                bestSpeed = currentSpeed;
                bestCache = userCache;
            }
            DownLoadMIDlet.getInstance().displayable.setCurrent(resultForm);
            resultForm.append("第" + countIndex + "次下载结果统计如下:");
            resultForm.append("总共下载大小为:" + musicSize / UNIT_BYTE + "(k)");
            resultForm.append("下载耗时:" + (endTime - startTime) + "毫秒" +
                              "-->" +
                              second + "秒");
            resultForm.append("下载速度(k/s):" + currentSpeed + "k/秒/n");
            if (currentSpeed * 10 >= per_second_unit) {
                resultForm.append("该下载速度播放音乐不会卡");
            } else {
                resultForm.append("该下载速度播放音乐会卡");
            }
        }

上面的列子中你可以把下载的数据不是真正的音乐数据打印出来,它应该是以"<?xml"开头的。如:
                      byte[] data = new byte[recevie_length];
                       System.arraycopy(buffer, 0, data, 0, recevie_length);
                        try {
                            String oldStr = new String(data, "UTF-8");
                           resultForm.append(oldStr);
                        } catch (Exception e) {
                           e.printStackTrace();
                      }

 类似资料: