I am new in Android.I have a global variable in Main class:
public class MainActivity extends AppCompatActivity {
private int lenghtOfFile;
I'v created a AsyncTask to connect Internet and get file size from download link:
private class GetSizeOfFile extends AsyncTask<String, Void,Integer> {
@Override
protected Integer doInBackground(String... stringURls) {
String stringURl = stringURls[0];
Log.i("===", "doInBackground: " + stringURl);
try {
URL url = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
lenghtOfFile = connection.getContentLength();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return lenghtOfFile;
}
@Override
protected void onPostExecute(Integer lenghtOfFile) {
super.onPostExecute(lenghtOfFile);
Log.i("====", "size of file in Thread: " + lenghtOfFile);
}
}
when I logged in thread my global variable had been initialized:
Log.i("====", "size of file in Thread: " + lenghtOfFile);
but im my method that i have executed this thread my global variable had not been initialized ant its value is 0?
new GetSizeOfFile().execute(stringUrl);
Log.i("====", "size of file after Thread: " + lenghtOfFile);`
my logcat:
02-22 13:20:26.516 8167-8167/com.example.manifest.simplefiledownloadmanager I/===: addURLToList: http://cdn.p30download.com/?b=p30dl-software&f=VLC.Media.Player.v2.2.4.x64_p30download.com.rar
02-22 13:20:26.516 8167-8167/com.example.manifest.simplefiledownloadmanager I/====: file name : ?b=p30dl-software&f=VLC.Media.Player.v2.2.4.x64_p30download.com.rar
02-22 13:20:26.516 8167-8167/com.example.manifest.simplefiledownloadmanager I/====: size of file after Thread: 0
02-22 13:20:26.516 8167-8306/com.example.manifest.simplefiledownloadmanager I/===: doInBackground: http://cdn.p30download.com/?b=p30dl-software&f=VLC.Media.Player.v2.2.4.x64_p30download.com.rar
02-22 13:20:32.836 8167-8167/com.example.manifest.simplefiledownloadmanager I/====: size of file in Thread: 33344550
after few second lenghtOfFile in thread is initialized. how can i do some work that in this line I get right size of file from thread?
Log.i("====", "size of file after Thread: " + lenghtOfFile);