2011-10-20 26 views
0

我有一個WebView向我展示一個網頁,其中包含一個包含RSS源的URL,如何在頁面加載之前存儲URL?Android網頁內的解析URL

示例頁面:RSS通道的

此頁面是您的個人新聞頻道

網址: http://domain.com/news/feed.php?user_id=150&hash=7cde58a3f234929385068d3e64c13e

%%%%%編輯代碼進入PAGE %%%%

public class ilias extends Activity { 

WebView webView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     webView = (WebView)findViewById(R.id.webview); 

     BufferedReader bufferedReader = null; 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost request = new HttpPost("http://www.ilias.de/docu/login.php?client_id=docu"); 
     List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
     postParameters.add(new BasicNameValuePair("username", "stacked")); //this username 
     postParameters.add(new BasicNameValuePair("password", "overflow"));//works 


    try { 
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters); 
     request.setEntity(entity); 

     HttpResponse response= httpClient.execute(request); 

    bufferedReader = new BufferedReader(
      new InputStreamReader(response.getEntity().getContent())); 
    StringBuffer stringBuffer = new StringBuffer(""); 
    String line = ""; 
    String LineSeparator = System.getProperty("line.separator"); 
    while ((line = bufferedReader.readLine()) != null) { 
    stringBuffer.append(line + LineSeparator); 
    } 
    bufferedReader.close(); 

    Toast.makeText(ilias.this, 
    "Finished", 
    Toast.LENGTH_LONG).show(); 

    String webData = stringBuffer.toString(); 

    webView.setWebViewClient(new WebViewClient()); 
    webView.loadDataWithBaseURL("http://www.ilias.de/docu/",webData,"text/html","UTF-8","about:blank"); 
    String postData = "username=stacked&password=overflow"; 
    String url = "http://www.ilias.de/docu/login.php?client_id=docu"; 

    webView.postUrl(url, EncodingUtils.getBytes(postData, "base64")); 

    webView.loadUrl("http://www.ilias.de/docu/ilias.php?col_side=left&block_type=pdnews&cmd=showFeedUrl&cmdClass=ilpdnewsblockgui&cmdNode=i7:db:le&baseClass=ilPersonalDesktopGUI"); 

    } catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    Toast.makeText(ilias.this, 
    e.toString(), 
    Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    Toast.makeText(ilias.this, 
    e.toString(), 
    Toast.LENGTH_LONG).show(); 
    }finally{ 
    if (bufferedReader != null){ 
    try { 
    bufferedReader.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 
    } 

    } 
} 
+0

所以網頁包括URL,而不是應用程序本身,或代碼? –

+0

該網址在網頁上。我需要解析它,然後在我的應用程序中使用它 –

+0

您需要使用JSOUP等HTML解析器並使用標記解析url文本。 –

回答

1

你只需要創建一個自己的WebViewClient。然後,你可以保存URL它被調用前/圖所示:

private class MyWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

        // save url here 

     // false = open all urls with the embedded browser 
     return false; 
    } 
} 

現在的網頁視圖客戶端添加到網頁視圖:

webView.setWebViewClient(new MyWebViewClient()); 
+0

sry我想我沒有正確解釋。我已經有我的webviewclient加載包含網址爲html鏈接的網頁 –

+0

我的代碼應該在每次點擊webview內的鏈接時被調用。這不是你想要的嗎?鏈接不是正常的 Mark

+0

我不想點擊鏈接,我只是想解析網頁加載時。 –