如何从网址加载文字?

时间:2016-08-28 06:10:11

标签: android url bufferedinputstream

我想从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);

1 个答案:

答案 0 :(得分:0)