Android Ksoap2 web service for download/Upload

£可爱£侵袭症+ 提交于 2019-12-03 21:35:37

KSOAP is simply send request and getting responds. Lot of example are there in web search and get your suitable example. Here some examples Example 1

Example 2

Example 3

Example 4

For uploading and downloading the files through the SOAP web service I'm using the following method

Uploading:

  1. Convert your text file to Binary String
  2. Store it into the single string
  3. send it through the soap web service

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!