用android ksoap2 上传、下载数据

     在前面已经写了android如果使用ksoap2包来进行远程调用服务,调用服务方法的时候有时候需要传递参数,传递一些基本类型参数到没什么,如果在上传文件或下载数据的时候呢?大家都知道byte,byte是这样定义的,byte是通过网络传输信息的单位,网络上所有的信息都是以bit为单位传递的,一个bit代表一个0或1,8个bit等于1byte。当我们需要将手机本地的数据经过xml文件上传到服务端的时候,就需要把xml文件转换成byte才能成功上传。

  下面是代码以及过程:

首先要将文件转换成InputStream,本人在这里进行了文件压缩:

 1 // 对文件进行压缩
 2     public static byte[] compressToByte(File file) {
 3         try {
 4             InputStream inputStream = new FileInputStream(file);
 5             ByteArrayOutputStream out = new ByteArrayOutputStream();
 6             GZIPOutputStream gzip = new GZIPOutputStream(out);
 7             byte[] bytes = new byte[1024];
 8             int count = 0;
 9             while ((count = inputStream.read(bytes)) > -1) {
10                 gzip.write(bytes, 0, count);
11             }
12             gzip.finish();
13             gzip.close();
14             inputStream.close();
15             return out.toByteArray();
16         } catch (Exception e) {
17             // TODO: handle exception
18             e.getStackTrace();
19         }
20         return null;
21     }

压缩以后 返回的是byte[],服务端的参数类型也是byte[] ,目前转换成biye[]数组,以为就直接可以这样写就可以上传了么!

soapObject.addProperty("bytes", bytes);

这样上传是不成功的,我们需要在call之前用Base64进行register.

new MarshalBase64().register(envelope);

Base64网络上最常见的用于传输8Bit字节码的编码方式之一,当我们上传数据量很大的时候,像xml这种数据型文件,Base64编码可用于在HTTP环境下传递较长的标识信息。采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。关于Base64的知识,请到

http://baike.baidu.com/view/469071.htm去详细了解。

前面是上传数据,接下来是下载数据,服务端传回压缩后的xml字节数组,在bodyIn后,传回来的byte[]数据直接写成xml的话是乱码,这时需要以下操作:

一、如果是Base64需要先进行:

     byte[] bytes=Base64.decode(resulObject.getProperty(0).toString());

二、由于该文件是zip压缩文件的byte,需要对其写成zip文件

//把获取到的字节流写入zip文件
          File files=new File(activity.getFilesDir(), "huirongassets.zip");
          FileOutputStream fs=new FileOutputStream(files);
          fs.write(bytes, 0, bytes.length);
          fs.flush();
          fs.close();

三、对该zip文件进行解压:

 1  public byte[] unGZip(InputStream inputStream){
 2          byte[] b=null;
 3          try {
 4             //Gzip输入流
 5              GZIPInputStream gzip=new GZIPInputStream(inputStream);
 6             byte[] bytes=new byte[31240];
 7             int num=-1;
 8             ByteArrayOutputStream baos=new ByteArrayOutputStream();
 9             while((num=gzip.read(bytes,0,bytes.length))>-1){
10                 baos.write(bytes, 0, num);
11             }
12             b=baos.toByteArray();
13             baos.flush();
14             baos.close();
15             gzip.close();
16             return b;
17          } catch (Exception e) {
18             // TODO: handle exception
19             e.getStackTrace();
20         }
21          return null;
22      }

四、解压后返回解压后的字节数组 这时候byte[]是xml文件了:

1 File file=new File(activity.getFilesDir(), "dataversion.xml");
2          FileOutputStream outputStream=new FileOutputStream(file);
3          outputStream.write(bytes, 0, bytes.length);
4          outputStream.flush();
5          outputStream.close();

五、最后用xml解析器解析,关于解析器会后面再详细记录。android内嵌了pull解析器,java中还有SAX解析器,DOC解析器。

解析完毕后就可以获取服务端的数据了。

posted @ 2012-09-07 11:49  润羲  阅读(2293)  评论(0编辑  收藏  举报