Android解析网页内的URL

发布于 2024-12-11 04:12:49 字数 2966 浏览 0 评论 0原文

我有一个 WebView,它向我显示一个网页,其中包含包含 RSS 提要的 URL,如何在加载页面之前存储该 URL?

示例页面:

此页面是您的个人新闻频道

RSS 频道 URL: http://domain.com/news/feed.php?user_id=150&hash=7cde58a3f234929385068d3e64c13e

%%%%%编辑代码进入页面%%%%

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

    }
}

I have have a WebView that show me a webpage that include an URL that contains a RSS feed, how can i store the URL before the page is loaded?

Example page:

This page is your personal news channel

URL of RSS channel:
http://domain.com/news/feed.php?user_id=150&hash=7cde58a3f234929385068d3e64c13e

%%%%%EDIT CODE TO ENTER THE 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();
    }
   }
  }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2024-12-18 04:12:49

您只需要创建一个自己的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 客户端添加到 webview:

    webView.setWebViewClient(new MyWebViewClient());

You just need to create an own WebViewClient. Then you can save the url before it get called/shown:

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

now add your Webview client to the webview:

    webView.setWebViewClient(new MyWebViewClient());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文