从blackberry中的url获取数据
我有一个文件的url“http://www.example.com/123.aes“(它包含超过2MB的mp3文件数据)。现在我想从这个url获取数据。当我尝试使用http连接获取数据时,显示了一个错误-请求的实体太大。如何解决这个问题?。 我的代码如下:从blackberry中的url获取数据,blackberry,Blackberry,我有一个文件的url“http://www.example.com/123.aes“(它包含超过2MB的mp3文件数据)。现在我想从这个url获取数据。当我尝试使用http连接获取数据时,显示了一个错误-请求的实体太大。如何解决这个问题?。 我的代码如下: try { HttpConnection httpConn = (HttpConnection) Connector.open(httpURL, Connector.READ, true); final int iRespo
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文件。希望这对您有所帮助。尝试以下功能:
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");
最后我得到了答案-
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+"");
}
检查这些链接,
您已达到黑莓设备通过单个连接可以传输的最大数据量
-btsc.webapps.BlackBerry.com/btsc/KB10264,http://supportforums.blackberry.com/t5/Java-Development/HTTP-413-Request-Entity-Too-Large/ta-p/445983
-.@Rupak It’s not working on designes too…抱歉,之前评论中发布的链接并不是问题的答案。事实上,你可以查看Hasmukh的答案,.@Rupak iam也得到了相同的结果->“请求的实体太大”.iam也得到了相同的结果->调试时“请求的实体太大”,在第StringBuffer raw=new StringBuffer()行显示“java.lang.OutOfMemoryError calling toString”;