2014-04-13 36 views
1

我想下載文件格式的互聯網。我已給予所有權限,但我的應用程序提供了一些例外。這裏是我的源代碼..........從互聯網安卓文件下載給出了一個例外

private void write() 
     { 


      try { 
       URL url = new URL("http://wordpress.org/plugins/about/readme.txt"); 
       // URL url = new URL("http://androidsaveitem.appspot.com/downloadjpg"); 
       HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
       c.setRequestMethod("GET"); 

      // c.setReadTimeout(10000); // millis 
      // c.setConnectTimeout(15000); // millis 

       c.setDoOutput(true); 


       c.connect(); 

       String PATH = Environment.getExternalStorageDirectory() 
         + "/download/"; 

       File file = new File(PATH); 
       file.mkdirs(); 

       String fileName = "Sap.txt"; 


       File outputFile = new File(file, fileName); 
       FileOutputStream fos = new FileOutputStream(outputFile); 

       InputStream is = c.getInputStream(); 

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

        fos.write(buffer, 0, len1); 

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

       // } 
      } catch (IOException e) { 
       MessageBox(e.getMessage()); 
      } 

     } 



     //permission 
     <uses-permission android:name="android.permission.INTERNET" /> 
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

c.connect(); //這個connect()函數給出一個異常。我無法確定問題。

請別人幫我....

+0

什麼是異常,顯示你的日誌? –

+0

空指針異常........ –

+0

你在哪裏初始化'c'? –

回答

1

在主要功能

new AsyncTaskRunner().execute(""); 

之後添加以下主要功能

private class AsyncTaskRunner extends AsyncTask<String, String, String> 
     { 

      @Override 
      protected void onPostExecute(String result) { 
        try 
        { 

         MessageBox(result); 

         /* 
         String PATH = Environment.getExternalStorageDirectory() 
           + "/download/"; 

         File file = new File(PATH); 
         file.mkdirs(); 

         String fileName = "Sap.txt"; 

         File outputFile = new File(file, fileName); 
         FileOutputStream fos = new FileOutputStream(outputFile); 

         InputStream is = c; 

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

          fos.write(buffer, 0, len1); 

         } 
         fos.close(); 
         is.close(); 
         */ 
        } 
        catch(Exception ex) 
        { 
         MessageBox(ex.getMessage()+"error "); 
        } 

      } 

      @Override 
      protected String doInBackground(String... params) { 
       try 
       { 
        URL url = new URL("http://wordpress.org/plugins/about/readme.txt"); 
        // URL url = new 
        // URL("http://androidsaveitem.appspot.com/downloadjpg"); 
        HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
        c.setRequestMethod("GET"); 

        // c.setReadTimeout(10000); // millis 
        // c.setConnectTimeout(15000); // millis 

        //c.setDoOutput(true); 

        c.connect(); 
        InputStream is = c.getInputStream(); 

        String PATH = Environment.getExternalStorageDirectory() 
          + "/download/"; 

        File file = new File(PATH); 
        file.mkdirs(); 

        String fileName = "Sap.txt"; 

        File outputFile = new File(file, fileName); 
        FileOutputStream fos = new FileOutputStream(outputFile); 

        //InputStream is = c; 

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

         fos.write(buffer, 0, len1); 

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

        Log.v("esty", "Successfully"); 

        return "Successfully"; 
       } 
       catch(Exception ex) 
       { 
        Log.v("esty", ex.getMessage()); 
        return "failed"+ex.getMessage(); 
       } 
      } 

我希望它解決您的問題添加此! :)