从Internet获取.text,比较它并在AsyncTask中打开一个对话框

时间:2013-12-09 16:10:05

标签: android android-asynctask android-alertdialog

我想检查一下,如果我的应用程序有更新。为此,我在线提出了一个txt文件,只有一个最新的应用程序版本的整数。这应该与正在运行的应用程序版本进行比较,当有更新的版本时,打开一个对话框,供用户选择在浏览器中打开指向新.apk的链接或取消它。它给了我麻烦,我不知道该怎么做。谢谢你的协助!

AsyncTask:

public class UpdateCheck extends AsyncTask <Void, Void, Boolean> {


    @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) > 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();
        }

    }

}

以下是Log Cat:

 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

我刚刚在代码中添加了一些Log-Tags,看到它一直工作到Integer.parseInt(str)。目前str<html>,我不知道这来自何处,因为当我查找我的&#34;页面的源代码时,#34}它只是说数字......`

我通过将while循环更改为while ((str = in.readLine()) != null) { sb.append(str); }Integer.parseInt(sb.toString())来解决此问题。

1 个答案:

答案 0 :(得分:2)

strnull。你的while循环不正确。你继续循环UNTIL null,从而在Integer.parseInt(str)

时保证空值

您需要存储最后一个有效的非null String,以便稍后在解析int时使用。