webview找不到网页解决办法

在Android原生应用中使用webview加载H5页面时遇到无法显示的问题,排查后发现是由于使用了127.0.0.1的本地IP导致。正确做法是使用真实的IP地址(如192.x.x.x)来确保手机能够成功访问到网页内容。添加INTERNET权限和重写shouldOverrideUrlLoading方法并未解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做项目遇到原生android和H5混合的开发,部分页面需要用h5,js渲染显示,但是发现用h5的网页(可以在电脑中显示)无法在手机中载入,并报错:


     网上有很多说法,总结下来有

 1)没有加入INTERNET权限

 2)重写shouldOverrideUrlLoading方法: 

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
        startActivity(intent);

 但都有没有用。

 最后我的发现,是ip地址的问题,之前也遇到过,当时没有记下来,现在又忘了,囧。


这是我真实的ip地址(家里的wifi)

而前端页面,我是在H5builder里面跑的



发现问题了吗

127熟不熟悉?!!!

package yx.communitysocket.com.socket.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.alibaba.fastjson.JSONObject;

import yx.communitysocket.R;
import yx.communitysocket.com.socket.myJsBridge.jsbridge.BridgeHandler;
import yx.communitysocket.com.socket.myJsBridge.jsbridge.BridgeWebView;
import yx.communitysocket.com.socket.myJsBridge.jsbridge.BridgeWebViewClient;
import yx.communitysocket.com.socket.myJsBridge.jsbridge.CallBackFunction;
import yx.communitysocket.com.socket.utils.CustomTitleView;
import yx.communitysocket.com.socket.utils.Setting;

/**
 * Created by deng.jun on 2016/10/14.
 * 说明 : 跳转webView
 */
public class WebActivity extends Activity{
//    WebView mWebView;
    Context mContext;
    int RESULT_CODE = 0;
    BridgeWebView webView;
    ValueCallback<Uri> mUploadMessage;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == RESULT_CODE) {
            if (null == mUploadMessage){
                return;
            }
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        CustomTitleView.getCustomTitle(WebActivity.this, Setting.mWebTitle);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.socket_webview);
        mContext = WebActivity.this;

        webView = (BridgeWebView)findViewById(R.id.socket_webView);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true); //支持JS
        webSettings.setUseWideViewPort(true); //设置此属性,可任意比例缩放
        webSettings.setLoadWithOverviewMode(true); //自适应屏幕
        webSettings.setBuiltInZoomControls(true);
//        webSettings.setDomStorageEnabled(true);
        webSettings.setSupportZoom(true);
        webView.requestFocusFromTouch(); // 设置webview支持获取手势焦点,如用户需要输入账户、密码或其他情况
//       真实 http:192.168.0.106 127.0.0.1
        String urlH5 = "http://192.168.0.106:8020/mockStock/web/stockTrading/main.html?fid=trade";
//        String urlH5 = "http://10.8.8.251:8020/mockStock/web/stockTrading/main.html?fid=trade";
        webView.loadUrl(urlH5);

        webView.setWebChromeClient(new WebChromeClient() {
            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
                this.openFileChooser(uploadMsg);
            }

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
                this.openFileChooser(uploadMsg);
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                mUploadMessage = uploadMsg;
                pickFile();
            }
        });

    }

    boolean isJsBridgeUrl(String url) {
        return false;
    }

    public String getJsonData(){
        JSONObject jsonObject;
//        Bundle bundle = getIntent().getExtras();
//        String userID = bundle.getString("userId");
//        String involveID = bundle.getString("involveID");
        String userID = "3";
        String involveID = "6";

        jsonObject = new JSONObject();
        jsonObject.put("userID", userID);
        jsonObject.put("involveID", involveID);
        String jsonObj = jsonObject.toJSONString();
        return jsonObj;
    }

    public void pickFile() {
        Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
        chooserIntent.setType("image/*");
        startActivityForResult(chooserIntent, RESULT_CODE);
    }



    // 说明:按返回键时,不退出程序而是返回上一浏览页面
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

代码:


重点:

127是本机的ip地址,但是不是真实的ip地址,现在手机通过webview访问电脑的网页,必须要用真实ip,也就是192这货!

改url(127换成192)后,H5网页正常在手机显示了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值