【发布时间】: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);
【问题讨论】:
-
您需要使用
AsyncTask从 URL 中获取数据。见Make an HTTP request with android -
并确保您的代码不会抛出
MalformedURLException。 -
安卓新功能。请问我可以得到编辑后的代码吗!
标签: android url bufferedinputstream