简体   繁体   English

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

[英]Android Ksoap2 web service for download/Upload

I want to write a program using ksoap web service and download a file to android mobile from web service. 我想使用ksoap Web服务编写程序,然后从Web服务将文件下载到android mobile。 I must access a text file from web service and download it in the android mobile.Can some one help me with tutorial or links corresponding it 我必须从网络服务访问一个文本文件,然后将其下载到android mobile中。有人可以帮助我提供相关教程或相应链接

KSOAP is simply send request and getting responds. KSOAP只是发送请求并获得响应。 Lot of example are there in web search and get your suitable example. 网络搜索中有很多示例,您可以找到合适的示例。 Here some examples Example 1 这里有一些例子例子1

Example 2 例子2

Example 3 例子3

Example 4 例子4

For uploading and downloading the files through the SOAP web service I'm using the following method 为了通过SOAP Web服务上载和下载文件,我使用以下方法

Uploading: 上载:

  1. Convert your text file to Binary String 将您的文本文件转换为二进制字符串
  2. Store it into the single string 将其存储到单个字符串中
  3. send it through the soap web service 通过soap Web服务发送

Example

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

Download: 下载:

  1. Ask the server side developers to send the your file in the format of binary string 要求服务器端开发人员以二进制字符串格式发送文件
  2. Get the responds string and convert it as a file and store it in sdcard. 获取响应字符串并将其转换为文件并将其存储在sdcard中。

Example

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

The above method is working fine for me. 上面的方法对我来说很好。 May be you can find another best method. 也许您可以找到另一种最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM