public void checkUpdate(final boolean showMessage){
String url = "http://api.fir.im/apps/latest/57be8d56959d6960d5000327";
OkHttpUtils
.get()
.url(url)
.addParams("api_token", "a48b9bbcef61f34c51160bfed26aa6b2")
.build()
.execute(new StringCallback()
{
@Override
public void onError(Call call, Exception e, int id) {
if (showMessage) {
Toast.makeText(MainActivity.this, "检查更新失败!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onResponse(String response, int id) {
try {
JSONObject jsonObject = new JSONObject(response);
String version = jsonObject.getString("version");
String versionCode = CommonUtil.getVersionCode(MainActivity.this) + "";
if (versionCode.compareTo(version) < 0){
showUpdateDialog(jsonObject.getString("name"),jsonObject.getString("versionShort"),jsonObject.getString("changelog"),jsonObject.getString("update_url"),MainActivity.this);
}else{
if (showMessage) {
Toast.makeText(MainActivity.this, "已经是最新版本!", Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
if (showMessage) {
Toast.makeText(MainActivity.this, "检查更新失败", Toast.LENGTH_SHORT).show();
}
e.printStackTrace();
}
}
});
}
public static void showUpdateDialog(final String name, String version, String changelog, final String updateUrl, final Context context) {
String title = "发现新版" + name + ",版本号:" + version;
new android.support.v7.app.AlertDialog.Builder(context).setTitle(title)
.setMessage(changelog)
.setPositiveButton("下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse(updateUrl); //指定网址
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW); //指定Action
intent.setData(uri); //设置Uri
context.startActivity(intent); //启动Activity
}
})
.show();
}
okhttp获取版本更新—实例
猜你喜欢
转载自blog.csdn.net/ly_xiamu/article/details/83653197
今日推荐
周排行