2012-04-16 44 views
0

我有一個Android的apk文件託管在iis 7,我想下載並從一個Android應用程序安裝。我主要依靠在this question的代碼,但我在InputStream is = con.getInputStream();當試圖下載apk文件時,文件不喜歡異常

得到一個fileNotFound例外,我能夠從我的桌面上都和手機(三星GALAXY Nexus)在瀏覽器中下載apk文件和我已將apk mime類型添加到iis。

這是我的代碼。

protected Void doInBackground(Void... params) { 
     try { 
      URL url = new URL("http://android.atomweb.net.au/CasLite.apk"); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 

      con.setRequestMethod("POST"); 
      con.setDoOutput(true); 
      con.connect(); 

      String PATH = Environment.getExternalStorageDirectory() + "/download/"; 
      File file = new File(PATH); 
      file.mkdirs(); 
      File outputFile = new File(file, "app.apk"); 
      FileOutputStream fos = new FileOutputStream(outputFile); 

      InputStream is = con.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, len1); 
      } 

      fos.close(); 
      is.close(); 

      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(
        Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), 
        "application/vnd.android.package-archive"); 

      startActivity(intent); 

     } catch (IOException e) { 
      Log.d("Error", e.getLocalizedMessage()); 
     } 
     return null; 

    } 

當我發佈一個破發點,並期待和連接後,連接響應代碼已經被提出,但在之前的getInputStream,其405

+0

嘗試[在IIS 7中添加MIME類型apk](http://technet.microsoft.com/en-us/library/cc725608(v = ws.10).aspx)。 – yorkw 2012-04-16 23:53:14

回答

0

難道不應該是一個GET,不是POST ?

+0

是的,我應該。我用兩個動詞都試過了,我得到了同樣的結果。 – 2012-04-16 23:28:39