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

Cra*_*mer 1 android web-services android-ksoap2

我想用ksoapWeb服务编写程序,并从web服务下载文件到android mobile.我必须从web服务访问一个文本文件并将其下载到android mobile.Can有人帮我提供教程或相应的链接

Raj*_*ram 7

KSOAP只是发送请求并得到响应.很多例子都在网络搜索中,并得到你合适的例子.这里有一些例子 例1

例2

例3

例4

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

上传:

  1. 将文本文件转换为二进制字符串
  2. 将其存储在单个字符串中
  3. 通过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);
Run Code Online (Sandbox Code Playgroud)

下载:

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

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();
Run Code Online (Sandbox Code Playgroud)

上面的方法对我来说很好.也许你可以找到另一种最好的方法.