-2

I am new in android and i ask a question about my thread that i create. I think it is stupid question but I am sorry.I have a onClick button listener. Its job is get the URL download link and stores in a variable.

   /**
 * this method invoke from setPositiveButton's dialog
 *
 * @param rootView
 */
private void addURLToList(View rootView) {

    editTextAddURL = (EditText) rootView.findViewById(R.id.editText_add_url);

    Log.i("===", "addURLToList: " + editTextAddURL.getText());
    stringUrl = editTextAddURL.getText().toString();

     *start GetSizeOfFile thread for getting size file and store
     * in lenghtOfFile variable
     */
    new GetSizeOfFile().start();

    Log.i("====", "size of file after Thread: " + lenghtOfFile);


}

I create a Thread because I want to get file size.

  private class GetSizeOfFile extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            URL url = new URL(stringUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            lenghtOfFile = connection.getContentLength();
            Log.i("====", "size of file in Thread: " + lenghtOfFile);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

everything is ok but when thread is started ,after few second my lenghtOfFile variable is initialized and I got 0 in lenghtOfFile in this Line Log.i("====", "size of file after Thread: " + lenghtOfFile); this is my logcat:

02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/===: addURLToList: http://dl2.soft98.ir/soft/a/Adobe.Shockwave.Player.12.2.7.197.IE.rar
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/====: file name : Adobe.Shockwave.Player.12.2.7.197.IE.rar
02-22 10:02:11.352 11333-11333/com.example.manifest.simplefiledownloadmanager I/====: size of file after Thread: 0
02-22 10:02:36.544 11333-11495/com.example.manifest.simplefiledownloadmanager I/====: size of file in Thread: 13524394

I want to get file's size from thread first.is it correct that I have to sleep the thread or exit standard way?sorry I am new in android

sayres kabir
  • 362
  • 4
  • 22
  • as usual ... create callback, call callback from "the end of thread", do the staff which needs results from Thread in the calback ... waiting for thread results inside `addURLToList` makes Thread useless (yeah, I know NetworkOnMainThreadExcpetion ... but that's why it was made ... for not blocking the main thread) – Selvin Feb 22 '17 at 11:00
  • I don't know why some people give minus to some post?I am new in android If you don't want to answer me then don't answer, why give minus!!!! – sayres kabir Feb 22 '17 at 11:08
  • @Selvin thank you. can you send a tutorial link about you way? – sayres kabir Feb 22 '17 at 11:09
  • Use the AsyncTask, to achieve the same. Checkout below example. http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1 – Vijaykumar Chiniwar Feb 22 '17 at 11:15
  • Use the AsyncTask, to achieve the same. Checkout below example. http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1 – Vijaykumar Chiniwar Feb 22 '17 at 11:24
  • @VijaykumarChiniwar thank you . – sayres kabir Feb 22 '17 at 11:25

1 Answers1

0

When working with threads you cannot assume their order of execution.

What happens in your case I presume is that while your new thread is waiting on the connection to be established, the original thread is being run with the uninitialized lenghtOfFile variable so the log looks like it does. Another possibility is that the new thread didn't even begin running when the lenghtOfFile=0 line was logged. That is just how threads work.

For this exact purpose the ASyncTask class exists in Android. Your code should be somewhat like this:

private class GetSizeOfFile extends AsyncTask<String, Void, Long> {

     // runs on a background thread
     protected Long doInBackground(String... stringUrls) {
         String stringUrl = stringUrls[0];
         try {
             URL url = new URL(stringUrl);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             long lenghtOfFile = connection.getContentLength();
             return lenghtOfFile;
         } catch (Exception e) {
             e.printStackTrace();
         }
         return -1;
     }

     // runs on main thread
     protected void onPostExecute(Long lenghtOfFile) {
        if (lenghtOfFile == -1) {
            // something went wrong
        } else {
            Log.i("====", "size of file: " + lenghtOfFile);
            // whatever else you want to do
        }
     }
}

new GetSizeOfFile().execute(stringUrl);
iMax531
  • 177
  • 1
  • 2
  • 15