0

I have an WebView that loads an local html file (auth.html).
auth.html has main.js included, which needs to make AJAX POST & GET requests.

The Problem

When a request is made, i always get an ERR_CONNECTION_TIMED_OUT. It's not a server problem because the requests work great in the website.

MainActivity.java

package com.netwok.global;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);

        myWebView.clearCache(true);
        myWebView.clearHistory();
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setAppCacheEnabled(false);
        myWebView.setWebContentsDebuggingEnabled(true);
        myWebView.loadUrl("file:///android_asset/home.html");


        myWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
}

Any help is appreciated

yanko
  • 164
  • 9
  • Have you added `WRITE_EXTERNAL_STORAGE` & `INTERNET` permission in `AndroidManifest.xml`? See also https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local – Ritesh Khandekar Nov 18 '19 at 08:16
  • I just added `WRITE_EXTERNAL_STORAGE` (i already had `INTERNET`), it wasn't working. That's because i was "spamming" my own server with many request because i'm testing it. With a VPN, all the requests work (only with the permission that you said), you can post it as a solution if you want – yanko Nov 18 '19 at 19:05

0 Answers0