安卓:下载 .html 并将其转换为字符串。

3

我需要从某个URL下载.html文件。我该怎么做?如何将其转换为字符串?

更新:

我不知道为什么你们要给我点踩。在iOS上,我只需要使用一个方法stringWithContentsOfURL:encoding:error:就可以得到所需的结果。我建议Android也有类似的方法。

4个回答

5

以下代码从链接下载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);
        }
    }
}

2
这个怎么样:
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) {    
    }
      }

1
你可以使用 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();

抱歉,我不需要第三方库的解决方案。顺便说一下,谢谢您的回复。 - user1248568

0

您可以使用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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接