hugeterry/UpdateFun

网络较差环境下检查更新失败

bonyz opened this issue · 1 comments

bonyz commented

`public class Update extends Thread {

private String result;
private String url = "http://api.fir.im/apps/latest/" + UpdateKey.APP_ID
        + "?api_token=" + UpdateKey.API_TOKEN;

public void run() {
    try {
        URL httpUrl = new URL(url);

        HttpURLConnection conn = (HttpURLConnection) httpUrl
                .openConnection();
        conn.setRequestMethod("GET");
        conn.setReadTimeout(3000);

        if (conn.getResponseCode() == 200) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String str;

            while ((str = reader.readLine()) != null) {
                sb.append(str);
            }
            result = new String(sb.toString().getBytes(), "utf-8");

            interpretingData(result);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void interpretingData(String result) {
    try {
        JSONObject object = new JSONObject(result);
        DownloadKey.changeLog = object.getString("changelog");
        DownloadKey.version = object.getString("versionShort");
        DownloadKey.apkUrl = object.getString("installUrl");
        Log.i("UpdateFun TAG",
                String.format("ChangeLog:%s, Version:%s, ApkDownloadUrl:%s",
                        DownloadKey.changeLog, DownloadKey.version, DownloadKey.apkUrl));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}`

主要是超时时间设置为3000,默认太小,可以设置成可配置的。

下个版本会适当提升,当时开发考虑到这个状况是觉得如果网络差,那用户选择更新的可能性也不高,是否开个配置接口给用户还在考虑中,因为这个值好像不是必须的
谢谢你的反馈😙