这里我试图从服务器上下载一个文件到SD卡上。代码如下图所示:从服务器下载文件
try{
FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/documents/Report.xml");
if (fconn.exists()) {
fconn.delete();
}
fconn.create();
add(new RichTextField("Deleted and created", Field.NON_FOCUSABLE));
HttpConnection connection = (HttpConnection)
Connector.open("http://127.0.0.1/xml/home.xml");
add(new RichTextField("Connection success", Field.NON_FOCUSABLE));
InputStream ds = connection.openInputStream();
byte[] data = new byte[ds.available()];
ds.read(data);
add(new RichTextField("Data read", Field.NON_FOCUSABLE));
OutputStream outStream = fconn.openOutputStream();
outStream.write(data);
outStream.close();
add(new RichTextField("Data written succesfully", Field.NON_FOCUSABLE));
fconn.close();
}
catch (Exception e) {
// TODO: handle exception
}
的问题是,在emulatora我看到的第一个两个消息,即
- 删除并创建
- 连接成功
但经过,没有给出数据被写入的消息。 XML文件的大小是1KB。 可以帮我解决这个问题吗?我的代码中是否有任何问题或者是否存在其他问题?
感谢4早期回复。我已经测试了所有的NULL值,我的连接也是成功的,但问题出在Input Stream对象创建。所以请给我一个输入流的解决方案,以及如何使用上面提到的所有权限。 – AMUL 2012-03-22 12:08:31
inputstream行是实际建立HTTP连接的第一行。也许你正在超时。您应该让它至少运行3分钟,并记录异常。然后请张贴它。 – 2012-03-22 17:37:34
关于权限,我认为它们在模拟器中是不需要的。在真实设备中,用户在安装时设置权限(或稍后通过选项菜单)。 – 2012-03-22 17:39:02