【发布时间】:2012-11-21 04:51:10
【问题描述】:
我想使用ksoap 网络服务编写一个程序,并从网络服务下载一个文件到安卓手机。我必须从网络服务访问一个文本文件并将其下载到 android 手机中。有人可以帮助我提供相应的教程或链接
【问题讨论】:
-
我不知道,但我编写了一个程序来访问来自 Web 服务的值并做到了。但我不知道下载或上传文件。
标签: android web-services android-ksoap2
我想使用ksoap 网络服务编写一个程序,并从网络服务下载一个文件到安卓手机。我必须从网络服务访问一个文本文件并将其下载到 android 手机中。有人可以帮助我提供相应的教程或链接
【问题讨论】:
标签: android web-services android-ksoap2
KSOAP 只是发送请求并获得响应。网络搜索中有很多示例,并获得合适的示例。这里有一些例子
Example 1
为了通过 SOAP Web 服务上传和下载文件,我使用以下方法
例子
Uri uriString = Uri.parse(objBundle.get("").toString());
File file = new File(uriString.getPath());
FileInputStream objFileIS;
objFileIS = new FileInputStream(file);
ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
byte[] byteBufferString = new byte[1024];
for (int readNum; (readNum = objFileIS.read(byteBufferString)) != -1;)
{
objByteArrayOS.write(byteBufferString, 0, readNum);
system.out.println("read " + readNum + " bytes,");
}
byte[] byteBinaryData = Base64.encode((objByteArrayOS.toByteArray()), Base64.DEFAULT);
strAttachmentCoded = new String(byteBinaryData);
例子
byte[] bytes;
strBinaryPDFString = cursorGetBinaryString.getString(0);//Index position of the binary string in table
File createfile = new File(Environment.getExternalStorageDirectory(),"/Folder/");
createfile.mkdirs();
File outputFile = new File(createfile,"FileName.pdf");//creating temporary file in phone Memory
new FileOutputStream(outputFile);
bytes = Base64.decode(strBinaryPDFString,Base64.DEFAULT);
File filepath = new File(Environment.getExternalStorageDirectory(),"/Folder/FileName.pdf");
OutputStream pdffos = new FileOutputStream(filepath);
pdffos.write(bytes);//writing the binary string into the payslip.pdf temporary file
pdffos.flush();
pdffos.close();
上述方法对我来说效果很好。也许你可以找到另一种最好的方法。
【讨论】: