android webview不重定向到另一个页面水疗网站

jvidinwx  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(348)

我有一个网站,我希望该网站成为一个移动应用程序,因为它是响应。我决定把它作为一个webview,因为它更容易做,因为我不是一个移动开发人员。我的网站使用laravel+vuejs spa(单页应用程序)。但当我点击导航栏登录后,它不会将我重定向到下一页。有人能告诉我我错过了什么吗?它是关于我的网站还是仅仅是安卓系统的问题?
主活动.java

package com.example.carrental;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView web;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web = findViewById(R.id.webView);
        WebSettings webSettings = web.getSettings();
        webSettings.setJavaScriptEnabled(true);
        web.setWebViewClient(new Callback());
        web.loadUrl("http://52.74.70.6:90/");

    }

    private class Callback extends WebViewClient {
        @Override
        public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
            return false;
        }
    }
}

androidmanifest.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.carrental">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:networkSecurityConfig="@xml/network_security_config"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
0md85ypi

0md85ypi1#

您可以通过下面的代码在web视图中打开网站时查看url重定向

public class PaymentWebActivity extends Activity {

    private WebView webView;
    private String link = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paymentview);
        webView = (WebView) findViewById(R.id.webview);
        if (getIntent().getExtras() != null) {
            link = getIntent().getExtras().getString("link");
        }
        initWebView();
    }

    private void initWebView() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        WebViewClientImpl webViewClient = new WebViewClientImpl(this);
        webView.setWebViewClient(webViewClient);
        webView.loadUrl(link);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.webView.canGoBack()) {
            this.webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    public class WebViewClientImpl extends WebViewClient {

        public WebViewClientImpl(Activity activity) {
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            if (url.indexOf(link) > -1) return false;
            Log.e("Url :", url);
            checkUrl(webView, url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
            if (webResourceRequest.getUrl().toString().indexOf(link) > -1) return false;
            Log.e("Url :", webResourceRequest.getUrl().toString());
            checkUrl(webView, webResourceRequest.getUrl().toString());
            return false;
        }

    }

    private void checkUrl(WebView webView, String url) {
        if (checkSuccessUrl(url)) {
            webView.destroy();
            Intent returnIntent = new Intent();
            setResult(Activity.RESULT_OK,returnIntent);
            finish();
        } else if (checkErrorUrl(url)) {
            webView.destroy();
            Uri uri = Uri.parse(url);
            String errorMessage = uri.getQueryParameter("message");
            showConfirmDialog(errorMessage);
        }
    }

    private void showConfirmDialog(String errorMessage) {
        final Dialog dialog = new Dialog(PaymentWebActivity.this);
        Window w = dialog.getWindow();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_error);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(w.getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        w.setAttributes(lp);
        w.setBackgroundDrawableResource(android.R.color.transparent);

        TextView lbltitle = (TextView) w.findViewById(R.id.tvTitle);
        lbltitle.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, "APP Name"));
        lbltitle.setTypeface(CommonFunctions.getInstance().getRegular(PaymentWebActivity.this));

        TextView lblmessage = (TextView) w.findViewById(R.id.tvMessage);
        lblmessage.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, errorMessage));
        lblmessage.setTypeface(CommonFunctions.getInstance().getRegular(PaymentWebActivity.this));

        Button btnOk = (Button) w.findViewById(R.id.btnOk);
        btnOk.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, "OK"));
        btnOk.setTypeface(CommonFunctions.getInstance().getBold(PaymentWebActivity.this));
        btnOk.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });

        Button btnCancel = (Button) w.findViewById(R.id.btnCancel);
        btnCancel.setVisibility(View.GONE);
        /*btnCancel.setText(CommonFunctions.getInstance().getArabicText(PaymentWebActivity.this, "Cancel"));
        btnCancel.setTypeface(CommonFunctions.getInstance().getBold(PaymentWebActivity.this));
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });*/

        dialog.setCancelable(false);
        dialog.show();
    }

    private boolean checkSuccessUrl(String url) {
        return url.contains("success.php");
    }

    private boolean checkErrorUrl(String url) {
        return url.contains("error.php");
    }
}

成功.php
错误.php
这两个文件是在服务器端创建的,因此当checkurl找到这些文件时,我们可以根据url执行我们的逻辑。您也可以根据找到的页面转发页面。

相关问题