我有一些wep应用程序,有些页面必须执行ajax请求来获取和更新页面而不刷新页面。
当我使用android WebView加载这个Web应用程序时我的问题。请求ajax的所有页面都不会更新页面,这意味着ajax请求不起作用。
这是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");
}
并且android.html有ajax请求。 Android应用程序工作正常并加载android.html但没有获取ajax数据
最后我在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">
在响应中与android webview冲突并且不能与jquery选择器一起工作......不知道为什么!!!
但是当我从响应页面中删除上面的代码并且它的ajax工作正常。
P.S:我的所有页面都是xhtml而不是html。
<uses-permission android:name="android.permission.INTERNET" />
。http://192.168.1.236:8080
以外的某个站点发出请求,并且该站点不允许跨源XMLHTTPRequests,则由于WebView的安全限制,这些请求将失败。试试这个
WebView web=(WebView) findViewById(R.id.webView1);
web.getSettings().setJavaScriptEnabled(true);
您可以将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;
}
}
}