3

I need to download .html file from some URL. How can I do it? And how can I convert it to String?

UPDATED:

I have no idea why you downvoting. I can get the desired result on iOS by only using one method stringWithContentsOfURL:encoding:error:. And I suggested that Android has similar. method

0

4 Answers 4

4

Code below downloads html page from link, and return html page converted to string in completion callback

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); } } } 
Sign up to request clarification or add additional context in comments.

Comments

2

How's this:

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

Comments

1

You can use http://jsoup.org library or

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

and covert Input Stream to 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(); 

1 Comment

Sorry, but I don't need a solution from third-party libraries. BTW thx for respond.
0

You could use HttpURLConnection, streams, and a ReadableByteChannel.

I feel this helps down the line for adding request info to the connection.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.