Android强制更新

时间:2018-03-12 09:34:55

标签: android

我的一个项目遇到了崩溃。此问题在我的强制更新代码中。

> Caused by java.lang.NullPointerException: Attempt to invoke virtual
> method 'java.lang.String org.jsoup.nodes.Element.ownText()' on a null
> object reference
>        at com.****.android.****.activities.ForceUpdate.doInBackground(ForceUpdate.java:42)
>        at com.****.android.****.activities.ForceUpdate.doInBackground(ForceUpdate.java:23)
>        at android.os.AsyncTask$2.call(AsyncTask.java:295)
>        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
>        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
>        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
>        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
>        at java.lang.Thread.run(Thread.java:818)

我的代码是这样的

public class ForceUpdate extends AsyncTask<String,String,JSONObject> 

{

private String latestVersion;
private String currentVersion;
private Context context;
public ForceUpdate(String currentVersion, Context context){
    this.currentVersion = currentVersion;
    this.context = context;
}
@Override
protected JSONObject doInBackground(String... params) {

    try {
        latestVersion = Jsoup.connect("https://play.google.com/store/apps/details?id="+context.getPackageName()+"&hl=en")
                .timeout(30000)
                .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
                .referrer("http://www.google.com")
                .get()
                .select("div[itemprop=softwareVersion]")
                .first()
                .ownText();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return new JSONObject();
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
    if(latestVersion!=null){
        if(!currentVersion.equalsIgnoreCase(latestVersion)){
            // Toast.makeText(context,"update is available.",Toast.LENGTH_LONG).show();
            if(!(context instanceof LoginActivity)) {
                if(!((Activity)context).isFinishing()){
                    showForceUpdateDialog();
                }
            }
        }
    }

    super.onPostExecute(jsonObject);
}
public void showForceUpdateDialog(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context,
            R.style.Theme_AppCompat_Dialog));

    alertDialogBuilder.setTitle(context.getString(R.string.youAreNotUpdatedTitle));
    alertDialogBuilder.setMessage(context.getString(R.string.youAreNotUpdatedMessage) + " " + latestVersion + context.getString(R.string.youAreNotUpdatedMessage1));
    alertDialogBuilder.setCancelable(false);
    alertDialogBuilder.setPositiveButton(R.string.updates, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName())));
            dialog.dismiss();

        }
    });
    alertDialogBuilder.show();
}}

1 个答案:

答案 0 :(得分:0)

我们也在使用Jsoup来获取最新的应用版本。对我们来说它运作正常。以下是我的代码 -

        @Override
    protected String doInBackground(Void... voids) {
        try {
            latestVersion = Jsoup.connect(context.getString(R.string.play_store_url)
                    + BuildConfig.APPLICATION_ID)
                    .timeout(TIMEOUT)
                    .referrer(context.getString(R.string.google))
                    .get()
                    .select(context.getString(R.string.css_parser))
                    .first()
                    .ownText();
        } catch (Exception e) {
            return newVersion;
        }
    }

唯一不同的是,userAgent。尝试删除它。