需要使用webservice传输.apk文件

发布于 2024-12-15 18:29:25 字数 2391 浏览 5 评论 0原文

我已将我的 apk 文件转换为字节数组并使用 Web 服务发送它,如下

 [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 文件恢复为 SD 卡中的 .apk 文件进行安装。

I have converted my apk file into byte array and send it using webservice as follows

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

I have consumed the web service using ksoap protocol in my android application as bytes of array as given below

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

The problem is that I am not getting the exact apk file after converting byte array[] back to file.

Will anyone please review the code and please tell me is there any bug in this.

My need to get the converted byte[] apk file back to .apk file in the sdcard for installation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-12-22 18:29:25

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

请尝试以下操作:

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

response.toString() is likely not a String representation of your APK.

Try the following:

SoapObject result = (SoapObject)envelope.bodyIn;
byte[] b=result.toString().getBytes();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文