从黑莓手机网站获取数据

时间:2012-11-20 05:54:49

标签: blackberry

我有一个文件的网址“http://www.example.com/123.aes”(它包含一个超过2MB的mp3文件的数据)。现在我想从这个网址获取数据。当我尝试使用http连接获取数据时,它显示错误 - 请求的实体太大。如何解决这个问题呢 ?。 我的代码如下 -

 try {
    HttpConnection httpConn = (HttpConnection) Connector.open(httpURL, Connector.READ, true);
    final int iResponseCode = httpConn.getResponseCode();
    Dialog.alert(iResponseCode+"");
    InputStream is = httpConn.openInputStream();
    byte[] data = net.rim.device.api.io.IOUtilities.streamToBytes(is);
    String result = new String(data);
    Dialog.alert(result);

    FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/pictures/"+System.currentTimeMillis()+".mp3", Connector.READ_WRITE);
    conn.create();
    OutputStream out = conn.openOutputStream();
    out.write(data);
    out.flush();
    out.close();
    conn.close(); 
   } catch (IOException e) {
    Dialog.alert(e+"");
    }

此外,我想将其保存为手机内存中的mp3文件。

2 个答案:

答案 0 :(得分:1)

希望这会对你有所帮助。尝试以下功能:

public static String getPage(String url) {    
    String response = "";
    try {
        HttpConnection conn = 
            (HttpConnection)Connector.open(url,Connector.READ_WRITE);
        InputStream input =conn.openInputStream(); 
        byte[] data = new byte[256];
        int len = 0;
        StringBuffer raw = new StringBuffer();
        while( -1 != (len = input.read(data))) {
            raw.append(new String(data, 0, len));
        }
        response = raw.toString();
        input.close();
    } 
    catch(Exception e) {
        Dialog.alert(e.toString());
    }
    return response;
}


调用此函数的示例

getPage("yourUrl;deviceside=true;interface=wifi");

答案 1 :(得分:1)

最后我得到了答案 -

ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(httpURL);
HttpConnection httpConn = (HttpConnection) connDesc.getConnection();
   try {
    httpConn.setRequestMethod(HttpConnection.GET);
    InputConnection inputConn = (InputConnection) httpConn;
    InputStream is = inputConn.openInputStream();
    byte[] data =IOUtilities.streamToBytes(is);
    Dialog.alert(original.toString());
    FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/pictures/"+System.currentTimeMillis()+".mp3", Connector.READ_WRITE);
    conn.create();
    OutputStream out = conn.openOutputStream();
    out.write(original);
    out.flush();
    out.close();
    conn.close();  
   } catch (IOException e) {
      Dialog.alert(e+"");
   }