1

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

4

1 回答 1

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

下载:

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

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

于 2012-11-21T05:12:50.933 回答