2013-02-16 102 views
0

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

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); 
+1

我敢打赌'resumeObject.getJSONObject( “的sObject”)的getString( “名称”)的toString()'不会导致像'HTTP://domain.tdl/path/file。 mp3'。 'getProtocolInstance()'失败,这意味着你不提供一个协议(http://)或协议是未知的。 – annih 2013-02-16 13:52:35

+0

,它返回要保存的文件的名称。在我的演示url =“http://127.0.0.1/relief/api/uploads/p17gnmso5duql0lnhevja15td5.mp3”和文件路径我想要它保存为即fileName =“root1/bogolako.mp3” – 2013-02-16 14:55:34

+0

我知道它的下载。 ...我认为保存是问题 – 2013-02-16 15:11:08

回答

2

Connector.open(String)将抛出IllegalArgumentException如果URL是无效的,所以我们真的需要请参阅确切您通过的网址。

由于Stack Overflow注释格式的工作方式,要分辨哪个URL有点难。为了将来的参考,如果你想在一个评论中粘贴一个确切的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,那么你的缺少该呼叫的协议。假设"/root1/"实际上是模拟器上的有效绝对路径,则应将其前缀fileName"file:///"相加。 。

See here for a J2ME file connection example

+0

thanx很多Nate ....我需要预先加入“file:///”。 thx一帮伙伴 – 2013-02-18 09:16:05

+1

@SirLojik,很高兴这是一件简单的事情:) – Nate 2013-02-18 09:18:07