我正在尝试开发一个应用程序,可以检查自身的更新是否可用,然后提示用户是否升级。我遇到麻烦之一就是能够从在线提取应用程序版本号。假设我的url是正确的并且div类实际存在,可以安全地假设,我的代码中的问题在哪里?以下是我的代码:
点击按钮调用更新
public String url = "http://www.myurl.com"
UPDATE.setOnClickListener(new View.OnClickListener() {
String versionString = "";
@Override
public void onClick(View v) {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(connManager.TYPE_WIFI);
if (mWifi.isConnected()) {
new VersionCheck().execute();
}
}
});
//If wifi is connected then try to pull the version from offline.
private class VersionCheck extends AsyncTask<Void, Void, Void>{
String stringVersion;
int convertedString;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(UpdateActivity.this);
progressDialog.setTitle("Checking Current Version Number");
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try{
Document document = Jsoup.connect(URL1).get();
Elements element = document.select("div.VERSION_NUM");
stringVersion = element.toString();
convertedString = Integer.parseInt(stringVersion);
}catch (IOException ex){
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
String versionName = "";
int cVersion;
try {
versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
try{
cVersion = Integer.parseInt(versionName);
if(convertedString > cVersion){
Toast.makeText(getApplicationContext(), "NEW Version :D", Toast.LENGTH_SHORT).show();
//Ideally create new dialog here to download the new version
}
}catch (NumberFormatException i){
i.printStackTrace();
}
} catch (PackageManager.NameNotFoundException e) {
}
}
}
有任何问题请问,我会尽可能多地发布信息。 谢谢大家!