webview网址的输入流为空白,但webview会在其视图中显示一些内容

时间:2014-06-23 18:47:21

标签: android android-webview

我需要阅读html内容而不是在webview上显示它,同时使用webview进行在线支付等webview进行一系列交易。一旦到达最后一页,我将检查onPageFinished回调中的URL并使URLConnection读取输入流。但这总是空洞的内容,最终的webview在屏幕上有一些内容。

public class mWebClient extends WebViewClient {
    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);

        // Utils.displayLogs("<<<<<< mydebug >>>>>>>", "onLoadResource : "
        // + url);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub

        super.onPageStarted(view, url, favicon);
        Utils.displayLogs("<<<<<< mydebug >>>>>>>", "Url : " + url);
        if (mProgressDialog != null && !mProgressDialog.isShowing())
            mProgressDialog.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub

        // Utils.displayLogs("<<<<<< mydebug >>>>>>>", "Url : " + url);
        if (url.endsWith("finalSuccessPage.aspx")
                || url.endsWith("finalErrorPage.aspx")) {
            try {
                List<String> vals = checkSuccessContent(url);
                if (vals != null && vals.size() > 0) {                      
                    Log.d("<<<<<<mydebug >>>>>>", "vals[0] : "
                            + vals.get(0));
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        super.onPageFinished(view, url);
        if (mProgressDialog != null && mProgressDialog.isShowing())
            mProgressDialog.dismiss();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    private List<String> checkSuccessContent(String url) throws IOException {
        List<String> vals = new ArrayList<String>();
        if (url.endsWith("finalSuccessPage.aspx")
                || url.endsWith("finalErrorPage.aspx")) {


            URL tUrl = new URL(url);
            URLConnection con = tUrl.openConnection();

            Reader r = new InputStreamReader(con.getInputStream());
            StringBuilder buf = new StringBuilder();
            while (true) {
                int ch = r.read();
                if (ch < 0)
                    break;
                buf.append((char) ch);
            }
            String str = buf.toString();
            if (str.contains(Constants.STR_SUCCESS)) {
                String[] lVals = str.split("~");
                if (lVals != null) {
                    for (int i = 0; i < lVals.length; i++) {
                        vals.add(lVals[i]);
                    }
                }
            }
        }

        return vals;
    }
}

提前致谢, JRH

0 个答案:

没有答案