2017-05-01 16 views
0

JSONファイルをダウンロードするためのAsyncTaskメソッドがあります。ダウンロードした後、postExecuteで私はリストビューを作成します。モバイルネットワークでAsyncTaskを使用してファイルをダウンロードできません

私の問題は、モバイルネットワークに接続しているときにこの方法が動作しないことです。私は別の携帯電話で試してみました、またはProgressDialogスピナーに詰まっているか、postExecuteでProgressDialogを却下しても何も表示されません。

しかし、電話機がWi-Fiネットワークに接続されている場合は問題ありません。結果は表示されます。

方法

public class JsonDownloader extends AsyncTask<String, Integer, String> { 

    private PowerManager.WakeLock mWakeLock; 
    private FileOutputStream out = null; 
    private File filename = null; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); 
     mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,getClass().getName()); 
     mWakeLock.acquire(); 
     mProgressDialog = new ProgressDialog(getActivity()); 
     mProgressDialog.setTitle("Preparing List..."); 
     mProgressDialog.setMessage("Please wait while list is populating..."); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setCancelable(true); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgress(100); 
     mProgressDialog.show(); 
     mProgressDialog.setContentView(R.layout.custom_progressdialog); 
     ProgressBar progressbar=(ProgressBar)mProgressDialog.findViewById(R.id.pbar); 
     progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#FF4081"), android.graphics.PorterDuff.Mode.SRC_IN); 
    } 


    @Override 
    protected String doInBackground(String... image_urls) { 
     InputStream input = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(image_urls[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 
      int fileLength = connection.getContentLength(); 
      input = connection.getInputStream(); 
      Log.i("in save()", "after mkdir"); 
      filename = new File(path+offlineJsonFileName +".txt"); 
      Log.i("in save()", "after file"); 
      out = new FileOutputStream(filename); 
      Log.i("in save()", "after outputstream"); 
      out.flush(); 
      out.close(); 
      Log.i("in save()", "after outputstream closed"); 
      out = new FileOutputStream(filename); 
      byte data[] = new byte[4096]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 
       total += count; 
       if (fileLength > 0) // only if total length is known 
        publishProgress((int) (total * 100/fileLength)); 
       out.write(data, 0, count); 
      } 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (out != null) 
        out.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 
      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 
    @Override 
    protected void onProgressUpdate(Integer... progress) { 
     super.onProgressUpdate(progress); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgress(progress[0]); 
    } 
    @Override 
    protected void onPostExecute(String args) { 
     mWakeLock.release(); 
     mProgressDialog.dismiss(); 
      ReadFile(); 
    } 
} 

そして、私はこのようにそれを実行します。

jsonDownloader = new JsonDownloader(); 
jsonDownloader.execute(url); 
+0

任意のlogcatエラーまたは警告で働かせましたか? –

+0

いいえ、何もありません.... – DastakWall

+0

キャッチブロックにe.printStackTrace()がありません!あなたはいくつかの例外をキャッチします! – greenapps

答えて

0
 out.flush(); 
     out.close(); 
     Log.i("in save()", "after outputstream closed"); 
     out = new FileOutputStream(filename); 

そのすべて削除します。なぜ出力ストリームを閉じて再度開くのですか?

書き込みが完了したら、flush()を実行します。

+0

返事ありがとうございます、私はあなたが言ったことをすべて試しましたが、問題はまだ残っています – DastakWall

0

私はそれは、次の

class JsonDownloaderNew extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     System.out.println("Starting download"); 
     mProgressDialog = new ProgressDialog(activity); 
     mProgressDialog.setTitle("Preparing List..."); 
     mProgressDialog.setMessage("Please wait while list is populating..."); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgress(100); 
     mProgressDialog.show(); 
     mProgressDialog.setContentView(R.layout.custom_progressdialog); 
     ProgressBar progressbar=(ProgressBar)mProgressDialog.findViewById(R.id.pbar); 
     progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#FF4081"), android.graphics.PorterDuff.Mode.SRC_IN); 
    } 

    @Override 
    protected String doInBackground(String... f_url) { 
     int count; 
     try { 
      System.out.println("Downloading"); 
      URL url = new URL(f_url[0]); 

      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // getting file length 
      int lenghtOfFile = conection.getContentLength(); 
      // input stream to read file - with 8k buffer 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 
      // Output stream to write file 
      File filename = new File(path+offlineJsonFileName +".txt"); 
      OutputStream output = new FileOutputStream(filename); 
      byte data[] = new byte[1024]; 
      long total = 0; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       // writing data to file 
       output.write(data, 0, count); 
      } 
      // flushing output 
      output.flush(); 
      // closing streams 
      output.close(); 
      input.close(); 
     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String file_url) { 
     System.out.println("Downloaded"); 
     mProgressDialog.dismiss(); 
     ReadFile(); 
    } 
} 
関連する問題