0

我有一个 javame 项目,我正在尝试创建一种方法来下载 mp3 文件并将其保存到手机。该方法如下所示,不幸的是它一直抛出 java.lang.illegalargument 异常。

    public void Download_KeyTest()
    {
        FileConnection file = null;
        OutputStream outStream = null;
        String resumeJSON = mPreferences.get("resume");
        JSONObject resumeObject;
        try {
            resumeObject = new JSONObject(resumeJSON);
            String key=resumeObject.get("code").toString();
            String inputStr=getTextField12().getString();
            if (inputStr.equals(key))
            {
            Enumeration roots = FileSystemRegistry.listRoots();
            String currentRoot = null;
            while (roots.hasMoreElements()) {
                currentRoot = (String) roots.nextElement();
                System.out.println(currentRoot);
                }
            HttpConnection hc = null;
            DataInputStream in = null;
            try {
            String url = d_url+resumeObject.getJSONObject("sObject").get("loc").toString();
            String sname = urlEncode(resumeObject.getJSONObject("sObject").getString("name").toString());
            hc = (HttpConnection)Connector.open(url);
            int length = (int)hc.getLength();
            byte[] data = null;
            if (length != -1) {
                switchDisplayable(null,getWaitScreen1());
                data = new byte[length];
                in = new DataInputStream(hc.openInputStream());
                in.readFully(data);
                }
                else {
                // If content length is not given, read in chunks.
                switchDisplayable(null,getWaitScreen1());
                int chunkSize = 512;
                int index = 0;
                int readLength = 0;
                in = new DataInputStream(hc.openInputStream());
                data = new byte[chunkSize];
                do {
                    if (data.length < index + chunkSize) {
                    byte[] newData = new byte[index + chunkSize];
                    System.arraycopy(data, 0, newData, 0, data.length);
                    data = newData;
                    }
                    readLength = in.read(data, index, chunkSize);
                    index += readLength;
                } while (readLength == chunkSize);
            length = index;
            }
            getWaitScreen1().setText("Download Complete");
try {
            // Get path to photos folder.
            String dirMusic = System.getProperty("fileconn.dir.memorycard.music");
            if(dirMusic == null) {
                dirMusic=currentRoot;
                //throw new Exception("Unable get music folder name");
            }

            String fileName = dirMusic + sname +".mp3";
            // Open file
            file = (FileConnection)Connector.open(fileName, 
                    Connector.READ_WRITE);
            // If there is no file then create it
            if(file.exists() == false) {
                file.create();
            }
            // Write data received from camera while making snapshot to file
            outStream = file.openOutputStream();
            outStream.write(data);
            System.out.println(file.availableSize());
            //file.setHidden(false);

            getWaitScreen1().setText("Song saved to music folder.");

        } catch(IOException ioe) {
           Alert alertx = new Alert("IO error", ioe.toString(), null, AlertType.ERROR);
           alertx.setTimeout(Alert.FOREVER);
           switchDisplayable(alertx,getDownloadVerifyKeyForm());
        } catch(Exception exc) {
           Alert alertx = new Alert("Error", exc.toString()+exc.getMessage(), null, AlertType.ERROR);
           exc.printStackTrace();
           alertx.setTimeout(Alert.FOREVER);
           switchDisplayable(alertx,getDownloadVerifyKeyForm());
        } finally {
            // Try to close file
            try {
                if(outStream != null) {
                    outStream.close();
                }
                if(file != null) {
                    file.close();
                }
            } catch(Exception exc) {
                // Do nothing 
                exc.printStackTrace();
            }
        }            
        }
        catch (Exception ex)
        {
        ex.printStackTrace();    
        }
            }
        } catch (Exception ex)
        {
        ex.printStackTrace();    
        }
    }

这是我得到的错误

java.lang.IllegalArgumentException
    at javax.microedition.io.Connector.getProtocolInstance(), bci=28
    at javax.microedition.io.Connector.open(), bci=24
    at javax.microedition.io.Connector.open(), bci=3
 - spinapp.SpinApp.Download_KeyTest(SpinApp.java:703)

代码中的第 703 行是这样的

hc = (HttpConnection)Connector.open(url);
4

1 回答 1

2

Connector.open(String)如果 URL 无效,将会抛出IllegalArgumentException,所以我们真正需要看到的是您传入的确切URL。

由于 Stack Overflow 注释格式的工作方式,很难分辨是哪个 URL。以供将来参考,如果您想将确切的 URL 粘贴到评论中,请将其格式化为代码,并在其周围带有单个刻度线。因此,很难判断您的url变量是否包含协议前缀。

但是,您的完整网址可能应该是

http://127.0.0.1/relief/api/uploads/p17gnmso5duql0lnhevja15td5.mp3

显然,在测试 URL 时,将 URL 粘贴到桌面浏览器中通常也是一个好主意,并确保它也可以在那里工作。此 URL (host=127.0.0.1) 表示您在与模拟器相同的机器上运行服务器。

(这又提出了一点……您说这是针对您问题中的电话的,但是 127.0.0.1 服务器地址只有在您在模拟器中运行时才有效)


编辑:再次查看您的评论后,我想知道该行是否没有发生异常:

     file = (FileConnection)Connector.open(fileName, 
                Connector.READ_WRITE);

如果fileName等于root1/bogolako.mp3,那么您缺少该调用的协议。您应该以 为前缀fileName"file:///"假设这"/root1/"实际上是您模拟器上的有效绝对路径。

有关 J2ME 文件连接示例,请参见此处

于 2013-02-17T21:55:31.527 回答