Github获取仓库最新Release版本号API

package me.chunsheng.hongbao.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import me.chunsheng.hongbao.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Util for app update task.
 */
public class UpdateTask extends AsyncTask<String, String, String> {
    private Context context;
    private boolean isUpdateOnRelease;
    //public static final String updateUrl = "https://api.github.com/repos/geeeeeeeeek/WeChatLuckyMoney/releases/latest";
    public static final String updateUrl = "https://api.github.com/repos/geeeee/WeChatLuckyMoney/releases/latest";

    public UpdateTask(Context context, boolean needUpdate) {
        this.context = context;
        this.isUpdateOnRelease = needUpdate;
        if (this.isUpdateOnRelease) Toast.makeText(context, "正在检查新版本……", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == 200) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                responseString = out.toString();
                out.close();
            } else {
                // Close the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (Exception e) {
            return null;
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        try {
            JSONObject release = new JSONObject(result);

            // Get current version
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            String version = pInfo.versionName;

            String latestVersion = release.getString("tag_name");
            boolean isPreRelease = release.getBoolean("prerelease");
            if (!isPreRelease && version.compareToIgnoreCase(latestVersion) >= 0) {
                // Your version is ahead of or same as the latest.
                if (this.isUpdateOnRelease)
                    Toast.makeText(context, R.string.update_already_latest, Toast.LENGTH_SHORT).show();
            } else {
                if (!isUpdateOnRelease) {
                    Toast.makeText(context, context.getString(R.string.update_new_seg1) + latestVersion + context.getString(R.string.update_new_seg3), Toast.LENGTH_LONG).show();
                    return;
                }
                // Need update.
                String downloadUrl = release.getJSONArray("assets").getJSONObject(0).getString("browser_download_url");

                // Give up on the fucking DownloadManager. The downloaded apk got renamed and unable to install. Fuck.
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl));
                browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(browserIntent);
                Toast.makeText(context, context.getString(R.string.update_new_seg1) + latestVersion + context.getString(R.string.update_new_seg2), Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (this.isUpdateOnRelease)
                Toast.makeText(context, R.string.update_error, Toast.LENGTH_LONG).show();
        }
    }

    public void update() {
        super.execute(updateUrl);
    }
}

 

时间: 2024-10-04 01:54:56

Github获取仓库最新Release版本号API的相关文章

shell脚本从文件里逐行对比最新的版本号

问题描述 shell脚本从文件里逐行对比最新的版本号 版本号是这样的 1.1.1.1-2.99.99.99 想用shell来对比出最新的版本号,求各位大侠帮忙,谢谢了! 解决方案 用substrhttp://vb2005xu.iteye.com/blog/1576327 解决方案二: 用AWK来查找每一行,获取各列数据,然后自己比较得到最大值 解决方案三: cat file | sort -t. -n -k 1,1 -k 2,2 -k 3,3 -k 4,4 解决方案四: 补充,最后一行就是最大值

ndk-通过NDK获取手机信息(版本号,sdk版本,包名,屏幕的宽高,系统时间,ip)

问题描述 通过NDK获取手机信息(版本号,sdk版本,包名,屏幕的宽高,系统时间,ip) 5C 通过NDK获取手机信息(版本号,sdk版本,包名,屏幕的宽高,系统时间,ip)求大神解答 解决方案 http://www.2cto.com/kf/201506/407260.htmlhttp://www.xuebuyuan.com/1006760.html

PHP获取百度最新事件

PHP获取百度最新事件,如下代码: function getBaiduHotKeyWord()     {         $templateRss = file_get_contents('http://top.baidu.com/rss_xml.php?p=shijian');         If (preg_match('/<table>(.*)<\/table>/is', $templateRss, $_description)) {         $templateR

php获取twitter最新消息的方法

 本文实例讲述了php获取twitter最新消息的方法.分享给大家供大家参考.具体实现方法如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php function get_status($twitter_id, $hyperlinks = true) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?c

获取天气信息调哪个API稳定并免费

问题描述 如题获取天气信息调哪个API稳定并免费最好能返回图片的,呈现出来比较形象,这个很很重要有没有做过这方面开发的大神,求教 解决方案 解决方案二: 解决方案三:这个不是免费的啊,只能测试使用5天解决方案四:引用1楼guwei4037的回复:这个不是免费的啊,只能测试使用5天解决方案五:免费的午餐没营养解决方案六:http://apistore.baidu.com/希望能帮到你解决方案七:weather.com或者tianqi.com都不错

如何将.md文件传到github的仓库?

问题描述 如何将.md文件传到github的仓库? 刚开始用github,用msysgit按该网址操作http://jingyan.baidu.com/article/f7ff0bfc7181492e27bb1360.html,可是输密码的时候输不了,是什么原因?还是这个方法不对,需要一些其他的配置? 解决方案 如果你是做Java的,可以直接用idea,对github上的项目进行操作. 本地修改之后,要用git add xx文件,然后才可以进行提交到远程仓库. 解决方案二: 先看你的远程仓库路径

图片-github获取commit之前内容

问题描述 github获取commit之前内容 我的项目放在github,需要进行了多次的commit.push.现在希望获取一份以前的一个版本,如何操作? 解决方案 commit修改内容到github 解决方案二: 直接git checkout选择某一个版本的序列号

php获取twitter最新消息的方法_php技巧

本文实例讲述了php获取twitter最新消息的方法.分享给大家供大家参考.具体实现方法如下: <?php function get_status($twitter_id, $hyperlinks = true) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1"); curl_setopt($c,

jQuery获取浏览器类型和版本号的方法_jquery

废话不多说,关键代码如下所示: $(document).ready(function(){ varbrow=$.browser; varbInfo=""; if(brow.msie){bInfo="MicrosoftInternetExplorer"+brow.version;} if(brow.mozilla){bInfo="MozillaFirefox"+brow.version;} if(brow.safari){bInfo="A