2012-08-28 68 views
0

場景:我正在從asp.net web服務中將文件下載到我的android應用程序中。 Web服務返回一個字節數組。我能夠將文件下載到特定位置。但是,當我在android系統它給消息運行它:「問題解析包」安裝從.net web服務下載的.apk文件的問題

這裏是我的web服務代碼和和Android代碼:

Web服務代碼:

[WebMethod] 
     public byte[] GetApkFile(string deviceID) 
     { 
       try 
       { 
         string path = "D:\\Users\\CHAITANYA\\Documents"; 
         string fileName = path + "\\sample.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代碼:

package com.example.dwnload; 

    import java.io.BufferedOutputStream; 
    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileOutputStream; 
    import java.io.FileReader; 
    import java.io.IOException; 

    import org.ksoap2.SoapEnvelope; 
    import org.ksoap2.serialization.MarshalBase64; 
    import org.ksoap2.serialization.SoapObject; 
    import org.ksoap2.serialization.SoapSerializationEnvelope; 
    import org.ksoap2.transport.HttpTransportSE; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.os.Environment; 
    import android.telephony.TelephonyManager; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class FileDownloadActivity extends Activity 
    { 
      /** Called when the activity is first created. */ 

     TextView tv; 
     Button dwnload; 

      @Override 
      public void onCreate(Bundle savedInstanceState) 
      { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        dwnload=(Button)this.findViewById(R.id.button1); 
        tv=(TextView)this.findViewById(R.id.textView1); 
        dwnload.setOnClickListener(new View.OnClickListener() 
        { 

       public void onClick(View arg0) 
       { 

        TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
          String deviceId = TelephonyMgr.getDeviceId(); 
          String SOAP_ACTION = "http://tempuri.org/GetApkFile";  
          String METHOD_NAME = "GetApkFile"; 
          String NAMESPACE = "http://tempuri.org/"; 
          final String URL = "http://10.0.2.2/Dashboard/Service.asmx"; 

          SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME); 
          SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
          //new MarshalBase64().register(envelope); 
          envelope.encodingStyle = SoapEnvelope.ENC; 
          request.addProperty("deviceID", deviceId); 
          //tv.setText(deviceId.toString()); 
          envelope.setOutputSoapObject(request); 
          envelope.dotNet = true; 
          envelope.setOutputSoapObject(request); 
          HttpTransportSE httpTransport = new HttpTransportSE(URL); 
          try 
          { 
           httpTransport.call(SOAP_ACTION, envelope); 
           //SoapObject response = (SoapObject)envelope.getResponse(); 
           Object response = envelope.getResponse(); 
           byte[] b=response.toString().getBytes(); 
           String s = new String(b); 

           try 
           { 
              String filePath = Environment.getExternalStorageDirectory()+ "/myappdir/" + "sample.apk" ; 

                  File file = new File(filePath); 
              file.getParentFile().mkdirs(); 
              file.createNewFile(); 

              BufferedOutputStream objectOut = new BufferedOutputStream(new FileOutputStream(file)); 
              objectOut.write(b); 
              objectOut.close(); 

              } 
           catch (Exception e) 
           { 
              e.printStackTrace(); 
           } 



         } 


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



    } 

回答

0

我建議你遵循一些調試步驟。

首先驗證您的web服務是否按預期工作。
您可以通過編寫一個基於.net的客戶端來完成此操作,該客戶端將下載您的文件並將其寫入磁盤。你可以做一個二進制比較,看你得到的文件。

如果您確實按照預期獲取了該文件,那麼您的web服務工作正常。

現在將該文件與使用android下載的文件進行比較。

這應該有助於您隔離錯誤。

+0

好吧,正如你所指出的,我將遵循上面的調試步驟..謝謝! –

1

你有一個行存在於Android

byte[] b=response.toString().getBytes(); 

應由

byte[] b=Base64.decode(response.toString()); 

被替換,因爲肥皂將信封它編碼爲< base64Binary的>
不要忘記import org.kobjects.base64.Base64;


剛剛發現有關.net部分的內容。你的文件讀取方法不將文件傳輸正確......這一翻譯ReadByte我用讀並通過陣列(作爲參考),然後返回正確的數據數組:

[WebMethod] 
public byte[] GetApk() 
{ 
    string file = "file name goes here"; 
    FileStream stream = File.OpenRead(file); 

    byte[] buffer = new byte[stream.Length]; 
    stream.Read(buffer, 0, (int)stream.Length); 

    return buffer; 
} 

工作對我來說.. 。現在我的應用程序可以自動更新;) 我希望它也適用於你

+0

我已經嘗試了這些步驟你提到的。我得到的反應,但與sbytes( - ),所以我有解析錯誤,同時安裝apk.Need的幫助! –