首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在android WebView net::ERR_UNKNOWN_URL_SCHEME中获取错误

在android WebView net::ERR_UNKNOWN_URL_SCHEME中获取错误
EN

Stack Overflow用户
提问于 2022-06-10 11:53:36
回答 1查看 313关注 0票数 0

我有一个WordPress网络商务网站,其中包括UPI交易,网站完美地工作在桌面和移动浏览器。我把上面提到的网站通过Android网页视图转换为APK,但不幸的是,在app中没有执行UPI事务部分。我收到一条错误消息。

网页不可用upi://pay?pa=xxxxxxx@okaxis&pn=Malayaliescart&am=25.00&cu=INR&tn=OrderID 3826处的网页无法加载,因为:net::ERR_UNKNOWN_URL_SCHEME

但是UPI在移动浏览器上工作。当我点击UPI部分时,浏览器会问我应该打开哪个UPI应用程序?

下面是密码。

代码语言:javascript
运行
复制
    String websiteURL = "https://www.xxxx.com/"; // sets web url
    private WebView webview;
    SwipeRefreshLayout mySwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
        {
            //if there is no internet do this
            setContentView(R.layout.activity_main);
            //Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();

            new AlertDialog.Builder(this) //alert the person knowing they are about to close
                    .setTitle("No internet connection available")
                    .setMessage("Please Check you're Mobile data or Wifi network.")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    //.setNegativeButton("No", null)
                    .show();

        }
        else
        {
            //Webview stuff
            webview = findViewById(R.id.webView);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setDomStorageEnabled(true);
            webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
            webview.loadUrl(websiteURL);
            webview.setWebViewClient(new WebViewClientDemo());

        }

        //Swipe to refresh functionality
        mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);

        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        webview.reload();
                    }
                }
        );
    }


    private class WebViewClientDemo extends WebViewClient {
        @Override
        //Keep webview in app when clicking links
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mySwipeRefreshLayout.setRefreshing(false);
        }
    }

    //set back button functionality
    @Override
    public void onBackPressed() { //if user presses the back button do this
        if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
            webview.goBack(); //go back in webview
        } else { //do this if the webview cannot go back any further

            new AlertDialog.Builder(this) //alert the person knowing they are about to close
                    .setTitle("EXIT")
                    .setMessage("Are you sure. You want to close this app?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .setNegativeButton("No", null)
                    .show();
        }
    }
    }



class CheckNetwork {

    private static final String TAG = CheckNetwork.class.getSimpleName();

    public static boolean isInternetAvailable(Context context)
    {
        NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (info == null)
        {
            Log.d(TAG,"no internet connection");
            return false;
        }
        else
        {
            if(info.isConnected())
            {
                Log.d(TAG," internet connection available...");
                return true;
            }
            else
            {
                Log.d(TAG," internet connection");
                return true;
            }

        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-10 12:00:14

shouldOverrideUrlLoading中查看给出的是什么url来加载,如果它从upi方案开始,那么使用startActivityACTION_VIEW以及整个uri集作为数据有效负载。同样,在本例中返回false,并且不调用loadUrl,只调用return true (因为给定的return true被处理,而不是由WebView处理)

代码语言:javascript
运行
复制
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("upi") {
        Uri uri = Uri.parse(url);
        Intent upiIntent = new Intent(Intent.ACTION_VIEW, uri);
        view.getContext().startActivity(upiIntent);
    }
    else view.loadUrl(url);
    return true;
}

这里中,您有一些示例说明如何使用给定的URI打开第三方应用程序,ale要注意并处理ActivityNotFoundException (当这种URI没有应用程序时,在本例中是UPI支付)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72573869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档