【问题标题】:Android Ksoap2 web service for download/Upload用于下载/上传的 Android Ksoap2 Web 服务
【发布时间】:2012-11-21 04:51:10
【问题描述】:

我想使用ksoap 网络服务编写一个程序,并从网络服务下载一个文件到安卓手机。我必须从网络服务访问一个文本文件并将其下载到 android 手机中。有人可以帮助我提供相应的教程或链接

【问题讨论】:

  • 我不知道,但我编写了一个程序来访问来自 Web 服务的值并做到了。但我不知道下载或上传文件。

标签: android web-services android-ksoap2


【解决方案1】:

KSOAP 只是发送请求并获得响应。网络搜索中有很多示例,并获得合适的示例。这里有一些例子 Example 1

Example 2

Example 3

Example 4

为了通过 SOAP Web 服务上传和下载文件,我使用以下方法

上传中:

  1. 将文本文件转换为二进制字符串
  2. 将其存储到单个字符串中
  3. 通过肥皂网络服务发送它

例子

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

下载:

  1. 请服务器端开发人员以二进制字符串格式发送您的文件
  2. 获取响应字符串并将其转换为文件并存储在 sdcard 中。

例子

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();

上述方法对我来说效果很好。也许你可以找到另一种最好的方法。

【讨论】:

  • Android Apple 我知道网络服务,但我想要一个上传/下载文本文件的示例
  • 链接关闭。请在任何地方重新更新它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2011-07-13
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多