title: Get a .txt from the Internet, compare it and open a dialog in an AsyncTask
body: <p>I want to check, if there is an update for my app. For this I put up an txt file online with just an integer of the latest app version. This should get compared with the running app version and when there is a newer version, open a dialog for the user to give the choice of open a link to the new .apk in the browser or to cancel it. It's giving me trouble and I don't know what to do. Thanks for helping me out! </p>
  
  <p>The AsyncTask:</p>
  
  <pre><code>public class UpdateCheck extends AsyncTask &lt;Void, Void, Boolean&gt; {
  
  
      @Override
      protected Boolean doInBackground(Void... params) {
          String str = null;
          try {
              // Create a URL for the desired page
              URL url = new URL("http://googledrive.com/host/0B62qO_dIVN_hQ2lGNmJudDFEdHc/ver.txt");
  
              // Read all the text returned by the server
              BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  
              while ((str = in.readLine()) != null) {
                  // str is one line of text; readLine() strips the newline character(s)
              }
              in.close();
          } catch (MalformedURLException e) {
          } catch (IOException e) {
          }
  
          PackageInfo packageInfo;
          int curVersionCode = 0;
          try {
              packageInfo = getPackageManager().getPackageInfo("de.grevius.hhgvertretungsplan", 0);
              curVersionCode = packageInfo.versionCode;
          } catch (NameNotFoundException e) {
              // TODO Automatisch generierter Erfassungsblock
              e.printStackTrace();
          }
  
          if (Integer.parseInt(str) &gt; curVersionCode) {
              return true;
  
          }
  
          return false;
      }
  
      @Override
      public void onPostExecute (Boolean bool){
          if(bool){
  
              AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              // Add the buttons
              builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int id) {
                             final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://googledrive.com/host/0B62qO_dIVN_hQ2lGNmJudDFEdHc/Vertretungsplan.apk"));
                             startActivity(intent); //activity.startIntent...
                         }
                     });
              builder.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int id) {
                             dialog.cancel();
                         }
                     });
              // Set other dialog properties
              builder.setMessage("Es ist eine neue Version der App verfügbar. Herunterladen?");
  
              // Create the AlertDialog
              AlertDialog dialog = builder.create();
              dialog.show();
          }
  
      }
  
  }
  </code></pre>
  
  <p>Here is the Log Cat:</p>
  
  <pre><code> 12-09 17:10:37.305: E/AndroidRuntime(20346): FATAL EXCEPTION: AsyncTask #1
  12-09 17:10:37.305: E/AndroidRuntime(20346): java.lang.RuntimeException: An error occured while executing doInBackground()
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.lang.Thread.run(Thread.java:856)
  12-09 17:10:37.305: E/AndroidRuntime(20346): Caused by: java.lang.NumberFormatException: Invalid int: "null"
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.lang.Integer.invalidInt(Integer.java:138)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.lang.Integer.parseInt(Integer.java:355)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.lang.Integer.parseInt(Integer.java:332)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at de.grevius.hhgvertretungsplan.MainActivity$UpdateCheck.doInBackground(MainActivity.java:204)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at de.grevius.hhgvertretungsplan.MainActivity$UpdateCheck.doInBackground(MainActivity.java:1)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  12-09 17:10:37.305: E/AndroidRuntime(20346):    ... 5 more
  </code></pre>
  
  <hr>
  
  <p>I just added some Log-Tags to the code and saw, that it is working until the <code>Integer.parseInt(str)</code>. At this time <code>str</code> is <code>&lt;html&gt;</code>, I don't know where this comes from, because when I look up the source code of my "page" it says just the number...`</p>
  
  <p>I solved it by changing the <code>while</code> loop to <code>while ((str = in.readLine()) != null) { sb.append(str); }</code> and <code>Integer.parseInt(sb.toString())</code>.</p>