Java Android:下载.html并将其转换为字符串
我需要从某个URL下载.html文件。我怎么做?如何将其转换为字符串 更新: 我不知道你为什么投反对票。我只能使用一种方法Java Android:下载.html并将其转换为字符串,java,android,html,string,parsing,Java,Android,Html,String,Parsing,我需要从某个URL下载.html文件。我怎么做?如何将其转换为字符串 更新: 我不知道你为什么投反对票。我只能使用一种方法stringWithContentsOfURL:encoding:error:在iOS上获得所需的结果。我建议Android也有类似的功能。方法这是怎么回事: URL url; InputStream is = null; DataInputStream dis; String line; String out = ""; try { url = new URL(
stringWithContentsOfURL:encoding:error:
在iOS上获得所需的结果。我建议Android也有类似的功能。方法这是怎么回事:
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) {
}
}
您可以使用HttpURLConnection、streams和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();
}
试试看{
URL测试=新URL(/*链接到您的资源*/);
HttpURLConnection httpcon=(HttpURLConnection)test.openConnection();
httpcon.addRequestProperty(“用户代理”、“Mozilla/5.0”);
ReadableByteChannel rbc=Channels.newChannel(httpcon.getInputStream());
FileOutputStream fos=新的FileOutputStream(/*此处文件输出*/);
fos.getChannel().transferFrom(rbc,0,1您可以使用库
或
并将输入流转换为字符串
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();
下面的代码从链接下载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);
}
}
}
公共类HTMLPageDownloader扩展异步任务{
公共静态接口HTMLPageDownloaderListener{
公共抽象void completionCallBack(字符串html);
}
公共HTMLPageDownloaderListener侦听器;
公共字符串链接;
公共HTMLPageDownloader(字符串aLink,HTMLPageDownloaderListener-Alitener){
listener=aListener;
link=aLink;
}
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
HttpClient=new DefaultHttpClient();
HttpGet请求=新的HttpGet(链接);
字符串html=“”;
试一试{
HttpResponse response=client.execute(请求);
输入流输入;
in=response.getEntity().getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(in);
StringBuilder str=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
str.append(行);
}
in.close();
html=str.toString();
}捕获(非法状态){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回html;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
如果(!isCancelled()){
completionCallBack(结果);
}
}
}
抱歉,我不需要第三方库的解决方案。顺便说一句,请使用thx进行回复。
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);
}
}
}