我需要从某个 URL 下载 .html 文件。我该怎么做?以及如何将其转换为字符串?
更新:
我不知道你为什么投反对票。我可以通过仅使用一种方法在 iOS 上获得所需的结果stringWithContentsOfURL:encoding:error:
。我建议Android也有类似的。方法
下面的代码从链接下载html页面,并在完成回调中返回转换为字符串的html页面
public class HTMLPageDownloader extends AsyncTask<Void, Void, String> {
public static interface HTMLPageDownloaderListener {
public abstract void completionCallBack(String html);
}
public HTMLPageDownloaderListener listener;
public String link;
public HTMLPageDownloader (String aLink, HTMLPageDownloaderListener aListener) {
listener = aListener;
link = aLink;
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(link);
String html = "";
try {
HttpResponse response = client.execute(request);
InputStream in;
in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line);
}
in.close();
html = str.toString();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return html;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (!isCancelled()) {
listener.completionCallBack(result);
}
}
}
这个怎么样:
URL url;
InputStream is = null;
DataInputStream dis;
String line;
String out = "";
try {
url = new URL("http://www.example.com/");
is = url.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((line = dis.readLine()) != null) {
out.append(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
}
}
您可以使用http://jsoup.org库或
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}finally {
urlConnection.disconnect();
}
并将输入流转换为字符串
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
br.close();
您可以使用 HttpURLConnection、流和 ReadableByteChannel。
我觉得这有助于向连接添加请求信息。
try {
URL test = new URL(/* link to your resource */);
HttpURLConnection httpcon = (HttpURLConnection) test.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/5.0");
ReadableByteChannel rbc = Channels.newChannel(httpcon.getInputStream());
FileOutputStream fos = new FileOutputStream(/* File output here */);
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
fos.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}