Allow remote ajax calls in an Android Webview + jquery mobile

后端 未结 4 1288
执笔经年
执笔经年 2021-01-02 20:13

I\'m developing an javascript/HTML application with jquerymobile which makes ajax requests to a remote server. The application works fine on Chrome (only launching chrome wi

相关标签:
4条回答
  • 2021-01-02 20:21

    I had the same problem and I fixed it by overriding shouldInterceptRequest in my WebViewClient. I intercept the ajax call and do it in java. You have to do the same for POST methods

    private class MyWebViewClient extends WebViewClient {
            @Override
            public void onPageFinished(WebView webView, String url) {
                Log.d("test", "onPageFinished");
                loadWebViewJavascriptBridgeJs(webView);
            }
    
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webview, WebResourceRequest webrequest)
            {
                Log.d("test", "shouldInterceptRequest");
               return this.handleRequest(webrequest.getUrl().toString());
            }
    
            @NonNull
            private WebResourceResponse handleRequest(@NonNull String urlString) {
                try {
                    URL url = new URL(urlString);
                    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                    connection.setRequestProperty("User-Agent", "");
                    connection.setRequestMethod("GET");
                    connection.setDoInput(true);
                    connection.connect();
    
                    InputStream inputStream = connection.getInputStream();
                    return new WebResourceResponse("text/json", "utf-8", inputStream);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                }
                catch (ProtocolException e) {
                    e.printStackTrace();
                    return null;
                }catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
    
            }
        }
    

    When I inticiate my webview, I instanciate my WebViewClient

     mWebView.setWebViewClient(new MyWebViewClient());
    
    0 讨论(0)
  • 2021-01-02 20:27

    In your AndroidManifest.xml, do you have this line :

    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
  • 2021-01-02 20:33

    I don't have enough points to post this as a comment, however, please have a look at the following:

    ajax working on some Android devices, not in other

    Specifically, the following is required on Chrome based webViews:

    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

    Edit: sorry, I just saw you're already doing this -- I tested this on my end and it seems to have solved my issue (was getting an access-control-allow-origin error when loading local links via ajax).

    0 讨论(0)
  • 2021-01-02 20:35

    Try this

    WebView web=(WebView) findViewById(R.id.webView1);
    web.getSettings().setJavaScriptEnabled(true);
    
    0 讨论(0)
提交回复
热议问题