Guest User

saveWeb

a guest
Jan 27th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. package com.example.saveweb3;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10.  
  11. import org.apache.http.HttpStatus;
  12.  
  13. import android.support.v7.app.ActionBarActivity;
  14. import android.os.AsyncTask;
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. import android.widget.TextView;
  20.  
  21. public class MainActivity extends ActionBarActivity {
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.        
  28.         TextView myTextView = (TextView) findViewById(R.id.helloW);
  29.         new Download().execute();
  30.         myTextView.setText("Done");
  31.     }
  32.  
  33.  
  34.     @Override
  35.     public boolean onCreateOptionsMenu(Menu menu) {
  36.         // Inflate the menu; this adds items to the action bar if it is present.
  37.         getMenuInflater().inflate(R.menu.main, menu);
  38.         return true;
  39.     }
  40.  
  41.     @Override
  42.     public boolean onOptionsItemSelected(MenuItem item) {
  43.         // Handle action bar item clicks here. The action bar will
  44.         // automatically handle clicks on the Home/Up button, so long
  45.         // as you specify a parent activity in AndroidManifest.xml.
  46.         int id = item.getItemId();
  47.         if (id == R.id.action_settings) {
  48.             return true;
  49.         }
  50.         return super.onOptionsItemSelected(item);
  51.     }
  52. }
  53.  
  54. class Download extends AsyncTask<Void, Void, Void> {
  55.  
  56.     @Override
  57.     protected Void doInBackground(Void... params) {
  58.        
  59.          try {       
  60.           File SDCardRoot = Environment.getExternalStorageDirectory();
  61.           String filename= "effortback.txt";
  62.           File file = new File(SDCardRoot + "/",filename);
  63.  
  64.           file.createNewFile();
  65.           FileOutputStream fileOutput = new FileOutputStream(file);
  66.  
  67.           URL url = new URL ("http://www.example.com/index.html");
  68.           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  69.           urlConnection.setRequestMethod("GET");
  70.           urlConnection.setDoOutput(false);
  71.           urlConnection.connect();
  72.          
  73.           InputStream inputStream;
  74.           int status = urlConnection.getResponseCode();
  75.  
  76.           if(status >= HttpStatus.SC_BAD_REQUEST)
  77.               inputStream = urlConnection.getErrorStream();
  78.           else
  79.               inputStream = urlConnection.getInputStream();
  80.          
  81.          
  82.           int totalSize = urlConnection.getContentLength();
  83.           int downloadedSize = 0;
  84.  
  85.           byte[] buffer = new byte[2048];
  86.           int bufferLength = 0;
  87.  
  88.           while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
  89.            fileOutput.write(buffer, 0, bufferLength);
  90.            downloadedSize += bufferLength;
  91.           }
  92.           fileOutput.close();
  93.           if(downloadedSize==totalSize) {
  94.         }
  95.  
  96.          } catch (MalformedURLException e) {
  97.           e.printStackTrace();
  98.          } catch (IOException e) {
  99.           e.printStackTrace();
  100.          }
  101.        
  102.         return null;
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment