java Android webview ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16022171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android webview ajax
提问by Mohamed Habib
I have wep application which some pages have to do ajax requests to get and update that pages without refresh the page.
我有 wep 应用程序,其中某些页面必须执行 ajax 请求才能在不刷新页面的情况下获取和更新该页面。
My problem when I use android WebView to load that wep application. all pages that request ajax doesn't update the page which means the ajax requests don't work.
当我使用 android WebView 加载该 wep 应用程序时出现的问题。所有请求 ajax 的页面都不会更新页面,这意味着 ajax 请求不起作用。
here is the code of MainActivity.java
这是 MainActivity.java 的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
webView = (WebView) findViewById(R.id.webView);
webView.clearCache(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
new AlertDialog.Builder(view.getContext())
.setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Toast.makeText(view.getContext(), "onPageStarted", Toast.LENGTH_SHORT).show();
super.onPageStarted(view, url, favicon); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onPageFinished(WebView view, String url) {
Toast.makeText(view.getContext(), "onPageFinished", Toast.LENGTH_SHORT).show();
super.onPageFinished(view, url); //To change body of generated methods, choose Tools | Templates.
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowContentAccess(true);
webSettings.setUseWideViewPort(true);
webView.loadUrl("http://192.168.1.236:8080/mobile/android.html");
}
and the android.html have ajax request. the android application works fine and load the android.html but without getting the ajax data
和 android.html 有 ajax 请求。android 应用程序工作正常并加载 android.html 但没有获取 ajax 数据
回答by Mohamed Habib
Finally I've found the answer after 3 Long days.
the problem was in the page that I request which have this code:
终于,我在 3 天后找到了答案。
问题出在我请求的具有以下代码的页面中:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
in the response which conflict with android webview and doesn't work with jquery selectors ... Don't know why!!!
在与 android webview 冲突并且不适用于 jquery 选择器的响应中......不知道为什么!!!
but when I removed the above code from response the page and its ajax works fine.
但是当我从响应中删除上述代码时,页面及其 ajax 工作正常。
P.S: all my pages are xhtml not html.
PS:我所有的页面都是 xhtml 而不是 html。
回答by Const
- Check if you have
<uses-permission android:name="android.permission.INTERNET" />
in your AppManifest.xml file. - If javascript in the loaded page makes requests to some site other than
http://192.168.1.236:8080
and that site does not allow Cross-Origin XMLHTTPRequests, then these requests will fail due to security restrictions of WebView.
- 检查
<uses-permission android:name="android.permission.INTERNET" />
您的 AppManifest.xml 文件中是否有。 - 如果加载页面中的 javascript 向某个站点发出请求,
http://192.168.1.236:8080
并且该站点不允许跨域 XMLHTTPRequests,那么由于 WebView 的安全限制,这些请求将失败。
回答by Danyial Shahid Ali
Try this
试试这个
WebView web=(WebView) findViewById(R.id.webView1);
web.getSettings().setJavaScriptEnabled(true);
回答by Vikrant Shah
You can add AJAX handler into your web-view for handle any website with AJAX.
您可以将 AJAX 处理程序添加到您的 Web 视图中,以便使用 AJAX 处理任何网站。
public class Main2Activity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
webView = (WebView) findViewById(R.id.webView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.GONE);
webView.getSettings().setJavaScriptEnabled(true);
initWebView();
loadURL();
}
private void loadURL() {
webView.loadUrl("www.google.com");
}
@SuppressLint("JavascriptInterface")
private void initWebView() {
webView.setWebChromeClient(new MyWebChromeClient(Main2Activity.this));
webView.addJavascriptInterface(new AjaxHandler(Main2Activity.this), "ajaxHandler");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
}
});
}
public class AjaxHandler {
private static final String TAG = "AjaxHandler";
private final Context context;
public AjaxHandler(Context context) {
this.context = context;
}
public void ajaxBegin() {
Log.e(TAG, "AJAX Begin");
Toast.makeText(context, "AJAX Begin", Toast.LENGTH_SHORT).show();
}
public void ajaxDone() {
Log.e(TAG, "AJAX Done");
Toast.makeText(context, "AJAX Done", Toast.LENGTH_SHORT).show();
}
}
private class MyWebChromeClient extends WebChromeClient {
Context context;
public MyWebChromeClient(Context context) {
super();
this.context = context;
}
}
}