Android:下载.html并将其转换为String

时间:2013-04-11 03:15:52

标签: java android html string parsing

我需要从某个URL下载.html文件。我该怎么做?我怎样才能将它转换为String?

更新:

我不知道为什么你贬低。只使用一种方法stringWithContentsOfURL:encoding:error:,我就可以在iOS上获得所需的结果。我建议Android有类似的。方法

4 个答案:

答案 0 :(得分: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);
        }
    }
}

答案 1 :(得分:1)

这是怎么回事:

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) {    
    }
      }

答案 2 :(得分: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();
}

并将Inputstream转换为String

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();

答案 3 :(得分: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();
}