Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to transfer .apk file using webservice

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.

like image 942
Arun Kumar Avatar asked Nov 15 '11 11:11

Arun Kumar


People also ask

How do I transfer APK files?

Just connect your smartphone to computer via USB cable and select “Media device” when prompted. Then, open your phone's folder on your PC and copy the APK file you want to install. Simply tap the APK file on your handset to facilitate installation. You can also install APK files from your phone's browser.


1 Answers

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

Try the following:

SoapObject result = (SoapObject)envelope.bodyIn;
byte[] b=result.toString().getBytes();
like image 69
Kevin Avatar answered Sep 28 '22 20:09

Kevin