需要使用webservice传输.apk文件

时间:2011-11-15 11:07:44

标签: android web-services file-upload apk

我已将我的apk文件转换为字节数组,并使用webservice发送,如下所示

 [WebMethod]
    public byte[] GetApkFile(string deviceID)
    {
        try
        {
            string path = ServiceHelper.GetTempFilePath();
            string fileName = path + "\\VersionUpdate.apk";
            FileStream fileStream = File.OpenRead(fileName);
            return ConvertStreamToByteBuffer(fileStream);          
        }
        catch (Exception ex)
        {
            throw ex;

        }

    }

      public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
    {
        int b1;
        System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
        while ((b1 = theStream.ReadByte()) != -1)
        {
            tempStream.WriteByte(((byte)b1));
        }
        return tempStream.ToArray();
    }

我在我的android应用程序中使用kso​​ap协议消耗了Web服务,如下面给出的数组字节

public void DownloadApkFile(String serverIPAddress,
        String deviceId) {

    String SOAP_ACTION = "http://VisionEPODWebService/GetApkFile";
    String OPERATION_NAME = "GetApkFile";
    String WSDL_TARGET_NAMESPACE = "http://VisionEPODWebService/";
    String SOAP_ADDRESS = "";
    SOAP_ADDRESS = "http://" + serverIPAddress
            + "/VisionEPODWebService/SystemData.asmx";
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
            OPERATION_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER10);
    new MarshalBase64().register(envelope); 
    envelope.encodingStyle = SoapEnvelope.ENC;
    request.addProperty("deviceID", deviceId);  
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    try {
        httpTransport.call(SOAP_ACTION, envelope);
        Object response = envelope.getResponse();
        byte[] b=response.toString().getBytes();

        String fileName = "/sdcard/" + "VersionUpdate" + ".apk";

       FileOutputStream fileOuputStream = 
                  new FileOutputStream(fileName); 
        fileOuputStream.write(b);
        fileOuputStream.close();           

    }

    catch (Exception exception) {
        exception.toString();           
    }

问题是我在将byte array []转换回文件后没有得到确切的apk文件。

有人请查看代码,请告诉我这是否有任何错误。

我需要将转换后的byte [] apk文件恢复到SD卡中的.apk文件进行安装。

1 个答案:

答案 0 :(得分:1)

response.toString()可能不是APK的字符串表示形式。

尝试以下方法:

SoapObject result = (SoapObject)envelope.bodyIn;
byte[] b=result.toString().getBytes();