Advertisement
elsemTim

Untitled

Feb 5th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package com.example.timurmuhortov.multithread_downloader.utils;
  2.  
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5.  
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9.  
  10. /**
  11.  * @author: timur.mukhortov
  12.  * date: 05.02.2018
  13.  * time: 23:18
  14.  * @LinkedIn: linkedin.com/in/timurmukhortov
  15.  **/
  16.  
  17.  
  18. public class MakeRequestTask extends AsyncTask<String, Void, String> {
  19.  
  20.     //Log
  21.     private String tagTas = "Task";
  22.  
  23.     public static final String REQUEST_METHOD = "GET";
  24.     public static final int READ_TIMEOUT = 15000;
  25.     public static final int CONNECTION_TIMEOUT = 15000;
  26.  
  27.     private String url;
  28.     private Integer countThread;
  29.     private Integer fileSize = 0;
  30.  
  31.     @Override
  32.     protected String doInBackground(String... params) {
  33.         url = params[0];
  34.         countThread = Integer.valueOf(params[1]);
  35.         try {
  36.  
  37.  
  38.             //Create a URL object holding our url
  39.             URL urlConnection = new URL(this.url);
  40.  
  41.             //Create a connection
  42.             HttpURLConnection connection = (HttpURLConnection)
  43.                     urlConnection.openConnection();
  44.  
  45.             //Set methods, timeouts, property
  46.             connection.setRequestMethod(REQUEST_METHOD);
  47.             connection.setReadTimeout(READ_TIMEOUT);
  48.             connection.setConnectTimeout(CONNECTION_TIMEOUT);
  49.             connection.setDoOutput(true);
  50.             connection.setRequestProperty("accept-encoding", "identity");
  51.             connection.setRequestProperty("content-encoding", "identity");
  52.  
  53.             //Connect to our url
  54.             connection.connect();
  55.  
  56.             //Get content size
  57.             fileSize = connection.getContentLength();
  58.             Log.i(tagTas, "File size = " + fileSize);
  59.  
  60.  
  61.         } catch (MalformedURLException e) {
  62.             e.printStackTrace();
  63.             Log.i(tagTas, e.getMessage());
  64.  
  65.         } catch (Exception e) {
  66.             e.printStackTrace();
  67.             Log.i(tagTas, "error!!!");
  68.             return "";
  69.         }
  70.         return null;
  71.     }
  72.  
  73.     //Result from background method in params
  74.     @Override
  75.     protected void onPostExecute(String s) {
  76.         super.onPostExecute(s);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement