【问题标题】:Need to transfer .apk file using webservice需要使用 webservice 传输 .apk 文件
【发布时间】:2011-12-29 10:33:45
【问题描述】:

我已将我的 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 应用程序中使用 ksoap 协议作为数组字节使用了 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();           
    }

问题是在将字节数组 [] 转换回文件后,我没有得到确切的 apk 文件。

请任何人检查代码并告诉我其中是否有任何错误。

我需要将转换后的 byte[] apk 文件取回 sdcard 中的 .apk 文件进行安装。

【问题讨论】:

    标签: android web-services file-upload apk


    【解决方案1】:

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

    尝试以下方法:

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

    【讨论】:

    • 我已经尝试过了...但问题是我正在使用 dot net webservice 转换文件。所以当涉及到 java 部分时,它无法解析回 apk 文件。如果是 Java到java通信就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2019-10-13
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多