2011-11-18 4 views
0

webview에로드 될 페이지의 HTML 코드를 가져와야하는 Android 애플리케이션이 있습니다. 이 코드는 웹 뷰에서 페이지가로드되고 그에 따라 작동하기 전에 가져와야합니다.webview에서로드 할 페이지의 HTML 코드를 가져 오는 방법은 무엇입니까?

나는 방법

"onPageStarted 공공 무효 (웹보기보기, 문자열 URL, 비트 맵 파비콘)"

에로드 될 URL을 캡처하려고하지만 HTML을 얻을 수있는 방법은 무엇입니까 webview에로드 될 페이지의 코드?

답변

0

나는 웹뷰 콘텐츠를하는 방법은 모르겠지만,

URL url; 
    try { 
     // url = new URL(data.getScheme(), data.getHost(), data.getPath()); 
     url = new URL("website url"); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(
       url.openStream())); 
     String line = ""; 
     while ((line = rd.readLine()) != null) { 
      text.append(line); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

이이 웹보기 콘텐츠를 작동하지 않습니다 그것은'유용 단지 웹 페이지 응답을 . –

0

그렇게하지, 웹 페이지

HttpClient httpClient = new DefaultHttpClient(); 
HttpContext localContext = new BasicHttpContext(); 
HttpGet httpGet = new HttpGet("website url"); 
HttpResponse response = httpClient.execute(httpGet, localContext); 
String result = ""; 

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
     response.getEntity().getContent() 
    ) 
); 

String line = null; 
while ((line = reader.readLine()) != null){ 
    result += line + "\n"; 

} 

두 번째 방법은 인의 코드를이 사용하고 HTML 데이터를 얻기 위해 모든 webview 함수를 찾았습니다. 나는 이것이 유용하다는 것을 알았다.

try { 
      URL twitter = new URL(
        "http://www.stackoverflow.com"); 
      URLConnection tc = twitter.openConnection(); 
      BufferedReader in = new BufferedReader(new InputStreamReader(
        tc.getInputStream())); 

      String htmlData = ""; 
      String line; 
      while ((line = in.readLine()) != null) { 
        htmlData = htmlData+"\n"+line; 
      } 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
2

활용 HttpClient를 :

public String httpGet(String url) throws URISyntaxException, 
     ClientProtocolException, IOException { 
     try { 

     HttpGet request = new HttpGet(); 

     request.setURI(new URI(url)); 
     response = client.execute(request); 

     htmlBody = EntityUtils.toString(response.getEntity()); 

     } catch (Exception ex) { 
     } 

     return htmlBody; 
    } 

그리고 웹보기에 그것을 보여주는 두 가지 방법이있다 하나 파일에서 (당신이 만드는 일 장치에 HTML을 가진 파일을 저장하기로되어있다 만족하는 오프라인) :

myWebView.loadUrl("file://" 
           + Environment.getExternalStorageDirectory() 
           + "/filename.html"); 

또는 HTML을 웹보기를 문자열을 공급 :

여기

당신이 나에 의해 작성된 전체 세션 객체, 당신이해야 할 수있는 몇 가지 기능이 일부되지 찾을 수 있습니다 :

http://droidsnip.blogspot.com/2011/10/custom-http-client-android.html#more

관련 문제