【问题标题】:android parse URL in a webviewandroid在webview中解析URL
【发布时间】:2011-10-21 13:55:07
【问题描述】:

我在 webview 中加载一个页面,我想解析 webview 中的 URL。

这是我使用 httppost 加载页面的代码。

URL 是 URL FEED:http://www.ilias.de/docu/feed.php............

此代码发出 httppost 请求并执行响应,然后将网页显示到 webview 中。

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.getSettings().setJavaScriptEnabled(true);

   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");
  webView.setWebViewClient(new WebViewClient() {
      public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
      handler.proceed() ;
      }
     });

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


    }
}

【问题讨论】:

    标签: android parsing url webview


    【解决方案1】:

    您可以通过在 WebViewClient 中使用 shouldOverrideUrlLoading 来完成您正在寻找的内容。

    前:

        webView.setWebViewClient(new WebViewClient() {
               public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
                     handler.proceed() ;
               }
    
               @Override
               public boolean shouldOverrideUrlLoading (WebView view, String url){
                      //Code to parse url
                      return false;
               }
        });
    

    【讨论】:

    • pd。我不想点击链接。只在页面加载时解析
    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2011-12-16
    • 2020-01-01
    相关资源
    最近更新 更多