Skip to main content
removeu 68 caracteres do conteúdo
Fonte Link
Syzoth
  • 100,5mil
  • 23
  • 169
  • 361
webView.setWebViewClient(new WebViewClient() {
   
    ...

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final String method = request.getMethod();
        final String url = request.getUrl().toString();

        Log.d(TAG, "processRequest: " + url + " method " + method);

        String ext = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(method);
            conn.setRequestProperty("Sample-Header", "hello");
            conn.setDoInput(true);
            conn.setUseCaches(false);

            Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

            //Permite o CORS
            responseHeaders.put("Access-Control-Allow-Origin", "*");

            //Permite ajustar o content-type nas requisições ajax
            responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

            return new WebResourceResponse(
                mime,
                conn.getContentEncoding(),
                conn.getResponseCode(),
                conn.getResponseMessage(),
                responseHeaders,
                conn.getInputStream()
            );

        } catch (Exception e) {
            Log.e(TAG, "shouldInterceptRequest: " + e);
        }

        return null;
    }
}
webView.setWebViewClient(new WebViewClient() {
   
    ...

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final String method = request.getMethod();
        final String url = request.getUrl().toString();

        Log.d(TAG, "processRequest: " + url + " method " + method);

        String ext = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(method);
            conn.setRequestProperty("Sample-Header", "hello");
            conn.setDoInput(true);
            conn.setUseCaches(false);

            Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

            //Permite o CORS
            responseHeaders.put("Access-Control-Allow-Origin", "*");

            //Permite ajustar o content-type nas requisições ajax
            responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

            return new WebResourceResponse(
                mime,
                conn.getContentEncoding(),
                conn.getResponseCode(),
                conn.getResponseMessage(),
                responseHeaders,
                conn.getInputStream()
            );

        } catch (Exception e) {
            Log.e(TAG, "shouldInterceptRequest: " + e);
        }

        return null;
    }
}
webView.setWebViewClient(new WebViewClient() {
   
    ...

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final String method = request.getMethod();
        final String url = request.getUrl().toString();

        Log.d(TAG, "processRequest: " + url + " method " + method);

        String ext = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(method);
            conn.setDoInput(true);
            conn.setUseCaches(false);

            Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

            //Permite o CORS
            responseHeaders.put("Access-Control-Allow-Origin", "*");

            //Permite ajustar o content-type nas requisições ajax
            responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

            return new WebResourceResponse(
                mime,
                conn.getContentEncoding(),
                conn.getResponseCode(),
                conn.getResponseMessage(),
                responseHeaders,
                conn.getInputStream()
            );

        } catch (Exception e) {
            Log.e(TAG, "shouldInterceptRequest: " + e);
        }

        return null;
    }
}
adicionou 242 caracteres ao conteúdo
Fonte Link
Syzoth
  • 100,5mil
  • 23
  • 169
  • 361

Então creio que conforme esta resposta do SOen pode fazer algo semelhante:]

webView.setWebViewClient(new WebViewClient() {
   
    ...

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final String method = request.getMethod();
        final String url = request.getUrl().toString();

        Log.d(TAG, "processRequest: " + url + " method " + method);

        String ext = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(method);
            conn.setRequestProperty("Sample-Header", "hello");
            conn.setDoInput(true);
            conn.setUseCaches(false);

            Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

            //Permite o CORS
            responseHeaders.put("Access-Control-Allow-Origin", "*");

            //Permite ajustar o content-type nas requisições ajax
            responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

            return new WebResourceResponse(
                mime,
                conn.getContentEncoding(),
                conn.getResponseCode(),
                conn.getResponseMessage(),
                responseHeaders,
                conn.getInputStream()
            );

        } catch (Exception e) {
            Log.e(TAG, "shouldInterceptRequest: " + e);
        }

        return null;
    }
}

Então creio que conforme esta resposta do SOen pode fazer algo semelhante:]

public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    final String method = request.getMethod();
    final String url = request.getUrl().toString();

    Log.d(TAG, "processRequest: " + url + " method " + method);

    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod(method);
        conn.setRequestProperty("Sample-Header", "hello");
        conn.setDoInput(true);
        conn.setUseCaches(false);

        Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

        //Permite o CORS
        responseHeaders.put("Access-Control-Allow-Origin", "*");

        //Permite ajustar o content-type nas requisições ajax
        responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

        return new WebResourceResponse(
            mime,
            conn.getContentEncoding(),
            conn.getResponseCode(),
            conn.getResponseMessage(),
            responseHeaders,
            conn.getInputStream()
        );

    } catch (Exception e) {
        Log.e(TAG, "shouldInterceptRequest: " + e);
    }

    return null;
}

Então creio que conforme esta resposta do SOen pode fazer algo semelhante:

webView.setWebViewClient(new WebViewClient() {
   
    ...

    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final String method = request.getMethod();
        final String url = request.getUrl().toString();

        Log.d(TAG, "processRequest: " + url + " method " + method);

        String ext = MimeTypeMap.getFileExtensionFromUrl(url);
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(method);
            conn.setRequestProperty("Sample-Header", "hello");
            conn.setDoInput(true);
            conn.setUseCaches(false);

            Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

            //Permite o CORS
            responseHeaders.put("Access-Control-Allow-Origin", "*");

            //Permite ajustar o content-type nas requisições ajax
            responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

            return new WebResourceResponse(
                mime,
                conn.getContentEncoding(),
                conn.getResponseCode(),
                conn.getResponseMessage(),
                responseHeaders,
                conn.getInputStream()
            );

        } catch (Exception e) {
            Log.e(TAG, "shouldInterceptRequest: " + e);
        }

        return null;
    }
}
Fonte Link
Syzoth
  • 100,5mil
  • 23
  • 169
  • 361

O problema que você deve estar tendo é com , não tem como desabilitar a segurança no webView para evitar estes problemas, se você tiver acesso a API então deve adicionar o header:

Access-Control-Allow-Origin: *

Se não tiver acesso aos códigos back-end da API então o que você pode fazer é criar uma especie de proxy para poder reescrever os headers

Então creio que conforme esta resposta do SOen pode fazer algo semelhante:]

public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    final String method = request.getMethod();
    final String url = request.getUrl().toString();

    Log.d(TAG, "processRequest: " + url + " method " + method);

    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);

    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setRequestMethod(method);
        conn.setRequestProperty("Sample-Header", "hello");
        conn.setDoInput(true);
        conn.setUseCaches(false);

        Map<String, String> responseHeaders = convertResponseHeaders(conn.getHeaderFields());

        //Permite o CORS
        responseHeaders.put("Access-Control-Allow-Origin", "*");

        //Permite ajustar o content-type nas requisições ajax
        responseHeaders.put("Access-Control-Allow-Headers: Content-Type");

        return new WebResourceResponse(
            mime,
            conn.getContentEncoding(),
            conn.getResponseCode(),
            conn.getResponseMessage(),
            responseHeaders,
            conn.getInputStream()
        );

    } catch (Exception e) {
        Log.e(TAG, "shouldInterceptRequest: " + e);
    }

    return null;
}

Não testei ainda mas a resposta é usar mesmo shouldInterceptRequest se não tiver acesso ao servers-side da API