【问题标题】:Read text file from url android从 url android 读取文本文件
【发布时间】:2016-11-03 18:09:19
【问题描述】:

我正在尝试使用 textFile 方法来通知用户我的应用程序有新的更新。我在 Playstore 和谷歌驱动器上都有该应用程序。我已经成功地以编程方式获取了我的应用程序的版本代码,现在的问题是读取我在谷歌驱动器上托管的文本文件中的版本代码。

我找到了以下代码,但是当我使用来自 google 驱动器的 url 时它没有返回任何内容。我错过了什么吗?

private static String getUrlContents(String theUrl)
{
    StringBuilder content = new StringBuilder();

    // many of these calls can throw exceptions, so i've just
    // wrapped them all in one try/catch statement.
    try
    {
        // create a url object
        URL url = new URL(theUrl);

        // create a urlconnection object
        URLConnection urlConnection = url.openConnection();

        // wrap the urlconnection in a bufferedreader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        String line;

        // read from the urlconnection via the bufferedreader
        while ((line = bufferedReader.readLine()) != null)
        {
            content.append(line + "\n");
        }
        bufferedReader.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return content.toString();
}

【问题讨论】:

  • to use the textFile approach to notify users of a new update。 ???那会是什么?没听说过。
  • 基本上,您在服务器上托管一个文本文件,其中包含应用程序的版本代码。当用户启动应用程序时,应用程序会检查在线托管文件中的版本代码,并将其与您可以通过编程方式获取的应用程序的当前版本代码进行比较。如果代码版本不同,那么我会启动一个带有更新提示的警报对话框,如果用户单击“更新”,我会将它们直接带到托管在 google drive 上的应用程序
  • content.append("Exception: " + e.getMessage() + "\n");。把它放在那个 catch 块中。

标签: java android bufferedreader


【解决方案1】:

我想我在另一篇文章中找到了解决方案(如评论中所建议):

你必须添加:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

onCreate 函数。

这是清单文件:

<uses-permission android:name="android.permission.INTERNET"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-13
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多