最近做项目遇到原生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网页正常在手机显示了。