2013-04-11 63 views
2

我需要從某個URL下載.html文件。我該怎麼做?我怎樣才能將它轉換爲字符串?Android:下載.html並將其轉換爲字符串

已更新:

我不知道你爲什麼downvoting。我可以通過僅使用一種方法stringWithContentsOfURL:encoding:error:在iOS上獲得所需的結果。我建議Android有類似的。方法

回答

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

這個怎麼樣:

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

對不起,但我不需要第三方庫的解決方案。 BTW thx迴應。 – user1248568 2013-05-10 12:51:47