2017-11-07 40 views
0

xlsファイルをダウンロードする必要があるURLがあります。私はコード化されたデータを投稿しており、応答はxlsファイルになります。しかし、私はこのxlsファイルを外部ストレージにダウンロードする方法がわかりません。私は、ネットワーク呼び出しを実行すると、私は java.io.FileNotFoundExceptionURLからファイルをダウンロード中にファイルが見つかりませんでした。

を持って、それが

Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference

言う私は、ファイルとしての応答を取得する方法は考えています。ここに私のコードです。

try{ 
     // TODO Auto-generated method stub 
     String data=URLEncoder.encode("job_id","UTF-8") 
       +"="+URLEncoder.encode(String.valueOf(responseText),"UTF-8"); 
     data+="&"+URLEncoder.encode("m_id","UTF-8")+"=" 
       +URLEncoder.encode(logedinUserId,"UTF-8"); 
     data+="&"+URLEncoder.encode("start","UTF-8")+"=" 
       +URLEncoder.encode(start,"UTF-8"); 
     data+="&"+URLEncoder.encode("end","UTF-8")+"=" 
       +URLEncoder.encode(endDate,"UTF-8"); 

     Log.e("data",""+data); 

     String text=""; 
     BufferedReader reader=null; 

     // Send data 
     try{ 

      // Defined URL where to send data 
      URL url=new URL("url here"); 

      // Send POST data request 

      URLConnection conn=url.openConnection(); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr=new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(data); 
      wr.flush(); 

      // Get the server response 

      reader=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb=new StringBuilder(); 
      String line=null; 

      // Read Server Response 
      while((line=reader.readLine())!=null){ 
       // Append server response in string 
       sb.append(line+"\n"); 
      } 


      text=sb.toString(); 
     }catch(Exception ex) 

     { 
      ex.printStackTrace(); 
     }finally{ 
      try{ 
       reader.close(); 
      }catch(Exception ex){ 
       ex.printStackTrace(); 

      } 
     } 

     Log.e("response x",""+text); 


    }catch(UnsupportedEncodingException e){ 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
+0

コードをデバッグしましたか?あなたのURLが何も返されていない可能性があります。そのため、ファイルが見つからないか、null例外が発生していますか? – Umair

+0

はい。私は郵便配達員とそれをテストしました。 – Bivin

+0

URLは正常に動作していますか?それはファイルを持っていますか? – Umair

答えて

0

[OK]を最初にあなたのUIスレッドがブロックされないようにasyncTaskにダウンロードするプロセスを配置する必要があります。次に、このコードを試すと、ファイルをダウンロードするのに役立ちます。

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

    /** 
    * Before starting background thread 
    * Show Progress Bar Dialog 
    */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     onCreateDialog(progress_bar_type); 
    } 

    /** 
    * Downloading file in background thread 
    */ 
    @SuppressWarnings("ResultOfMethodCallIgnored") 
    @Override 
    protected String doInBackground(String... f_url) { 
     String path = ""; 
     int responseCode = 0; 
     StringBuilder response = null; 
     try { 
      if (f_url[0] != null && !f_url[0].equals("")) 
       f_url[0] = f_url[0].replace("your url"); 


      URL obj = new URL(f_url[0]); 
      HttpURLConnection con = (HttpURLConnection) 
        obj.openConnection(); 
      con.setRequestMethod("GET"); 
      con.setDoOutput(true); 

      BufferedReader in = new BufferedReader(new 
      InputStreamReader(con.getInputStream())); 
      String inputLine; 
      response = new StringBuilder(); 

      while ((inputLine = in.readLine()) != null) { 
       response.append(inputLine); 
      } 
      in.close(); 

      System.out.println("Response : -- " + response.toString()); 

      //    path = connection.getInputStream().toString(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 

     return response != null ? response.toString() : null; 
//   return path; 
    } 

    /** 
    * Updating progress bar 
    */ 
    protected void onProgressUpdate(String... progress) { 
     // setting progress percentage 
     progressDialogForDownload.setProgress(Integer.parseInt(progress[0])); 
    } 

    /** 
    * After completing background task 
    * Dismiss the progress dialog 
    **/ 

    @Override 
    protected void onPostExecute(String fileURL) { 
     // dismiss the dialog after the file was downloaded 
     if (progressDialogForDownload.isShowing() && progressDialogForDownload != null) { 
      progressDialogForDownload.dismiss(); 
      progressDialogForDownload = null; 
     } 

     // you can open your file using intents or whatever you want to use. 
      startActivity(browserIntent); 
      Log.d("", "onButtonClickView: " + fileURL); 
     } 

    } 
} 

希望します。

関連する問題