用于下载/上传的 Android Ksoap2 Web 服务

标签 android web-services android-ksoap2

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

最佳答案

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

Example 2

Example 3

Example 4

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

上传:

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

例子

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 Ksoap2 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13486543/

相关文章:

java - Appium 驱动程序以编程方式获取应用程序包名称

java - 自动 JAX-RX 到 RAML

web-services - 在 weblogic 12.2 中的 Jersey 1.18

android - SoapFault - 故障代码 : '1062' faultstring: 'Shipping method is not available'

android - XMPPError : registration-required - auth 错误

android - 无法在 android 中加载 map

java - LibGDX:Android 设备上加载纹理缓慢(不断调用垃圾收集器)

javascript - 如何在 php 中通过 curl 发送请求负载

android - 如何在 Android 中使用 kSOAP 序列化自己创建的对象

android - 何时使用 Soapobject 和 SoapPrimitive