【问题标题】:How to load text from url?如何从 url 加载文本?
【发布时间】:2016-08-28 06:10:11
【问题描述】:

我想从 URL 加载文本,存储到字符串并将加载的文本附加到 String image_url = "http://example.com/example" + yourData;,如下所示。 我无法从 txt 文件中获取数据并附加到另一个 URL。需要帮助。谢谢!

try {
    // Create a URL for the desired page
    URL url = new URL("example.com/thefile.txt");

    // Read all the text returned by the server
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuilder builder = new StringBuilder();
    String str;
    while ((str = in.readLine()) != null) {
        // str is one line of text; readLine() strips the newline character(s)
        builder.append(str);
    }

    in.close();
    String yourData = builder.toString();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

// Loader image - will be shown before loading image
int loader = R.drawable.loader;
ImageView image = (ImageView) findViewById(R.id.image);
String image_url = "http://example.com/example" + yourData;
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(image_url, loader, image);

【问题讨论】:

标签: android url bufferedinputstream


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2016-12-30
  • 2019-05-08
  • 1970-01-01
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 1970-01-01
相关资源
最近更新 更多