Failed to execute ‘send’ on ‘XMLHttpRequest’: with a Java api

Hi,

It is returning the following error:
NetworkError: Failed to execute ‘send’ on ‘XMLHttpRequest’: failed to load “url…”
when I’m trying to access an API made in Java, only when the apk is generated on the VolteBuilder website. Generating and testing for Browser works correctly.The application was working correctly, it started to give an error in the last week, when I needed to generate a new version. Is anyone going through this?

Did you change anything else in your app when you made the new version? What platform?

Nothing is change, the platform is Android. The only thing that changed was when I went to generate the APK and there was an error in the config.xml, more specifically in the line below where I changed the value to 33.

preference name="android-targetSdkVersion" value="23"

The url call is very simple, I even made a test application with the lines below. the call works on the cell phone’s Borwser but when it generates apk it doesn’t work.

Function Button1_onclick()
    req=Ajax("http://" & Input1.value & "/ComandaTest/index.jsp?wLocal=1")
    Label2.value  = "http://" & Input1.value & "/ComandaTest/index.jsp?wLocal=1" & Chr(13)
    If req.status=200 Then
        Label2.value = req.responseText
    Else
      Label2.value  =  Label2.value  & "state: " & req.readyState & Chr(13)
      Label2.value  =  Label2.value  & "ret = " & req.statusText & Chr(13)
      Label2.value =  Label2.value  &  "Num Status = " & req.status  & Chr(13) 
      Label2.value =  Label2.value  &  "Erro = " & req.error  & Chr(13) 
    End If
End Function

I also have voltbuilder problem for 2 weeks, although the err is different. This is how I solve the problem. (1)the pwa png have to be 144x144.(2)the apk is save in my thumb drive. Hopes it helps… Best rdgs, tst

I can generate and install the apk on the cell phone, but when I call the url via the apk it gives the error reported at the beginning of the forum, thanks for the answer.

One more detail, as I generated a new apk to make the AJAX call, I was changing the config.xml as I was researching this problem on the internet, below is the config. Has anyone gone through this and have any new ideas?

<?xml version="1.0" encoding="UTF-8"?>
<widget 
xmlns = "https://www.w3.org/ns/widgets"
id = "com.nsbasic.{id}"
android-versionCode = "{phoneGapBuildCounter}"
ios-CFBundleVersion = "{phoneGapBuildCounter}"
version = "{version}">

<name>{title}</name>
<description>{description}</description>
<engine name='android' spec='12.0.0' />

<icon src='{icon}' />
<preference name='SplashScreenDelay' value='2000' />
<preference name='AutoHideSplashScreen' value='true' />
<preference name='AndroidWindowSplashScreenAnimatedIcon' value='resources/splashTemplate.png' />
<preference name='AndroidWindowSplashScreenBackground' value='#000000' />
<plugin name='cordova-plugin-splashscreen' source='npm' />

<preference name="permissions" value="none"/>
<!-- sample preference specifications -->
<!-- <preference name="autorotate" value="false" readonly="true"/> -->
<!-- <preference name="orientation" value="default" /> -->
<!-- <preference name="fullscreen" value="true" /> -->

<!-- Platforms: Customize as needed. -->
  <preference name="android-build-tool" value="gradle" />
  <preference name="phonegap-version" value="cli-10.0.0" />
  <preference name="pgb-builder-version" value="2" />
  <preference name="DisallowOverscroll" value="true" />
  <preference name="WebviewBounce" value="true" />
  <preference name="EnableViewPortScale" value="true" />
  <platform name="android">
    <preference name="android-minSdkVersion" value="22" />
    <preference name="android-targetSdkVersion" value="33" />
    <preference name="android-installLocation" value="auto" />
    <preference name="android-multiDexEnabled" value="true" />
    <preference name="Fullscreen" value="false" />
    <preference name="Orientation" value="portrait" />
    <preference name="android-signed" value="true" />
    <preference name="AndroidLaunchMode" value="singleTop" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-intent href="market:*" />
    <access origin='*'  />
    <icon platform="android" src="package-assets/ic_mdpi.png" density="mdpi" width="48" height="48" />
    <icon platform="android" src="package-assets/ic_hdpi.png" density="hdpi" width="72" height="72" />
    <icon platform="android" src="package-assets/ic_xhdpi.png" density="xhdpi" width="96" height="96" />
  </platform> 

<plugin name="cordova-plugin-statusbar" source="npm" />
  <preference name="StatusBarBackgroundColor" value="{phoneGapStatusBarColor}" />
  <preference name="StatusBarStyle" value="{phoneGapStatusBarStyle}" />

<plugin name="cordova-plugin-device" />
  <allow-navigation href="*" />
<access origin='*' allows-arbitrary-loads-for-media='true' allows-arbitrary-loads-in-web-content='true' allows-local-networking='true' />  <allow-intent href="*" />
<preference name="hostname" value="localhost" />
<preference name="AndroidInsecureFileModeEnabled" value="true" />



</widget>

Can you post a screen shot of the error?

Captura de Tela 2023-08-07 às 18.22.14

I thought it could be something in the Java url, but if I change to an IP that doesn’t even exist, the error is the same!

And before the change below in config.xml it worked.

<preference name="android-targetSdkVersion" value="33" />

The same url in the mobile browser.

According to this post,the sdk should be 31…

What prompted you to change to SDK 33 from the original value?

Are you able to use remote debugging to see any other relevant messages in the browser when this error occurs?

Actually I tried with 31, 32, 33 and also with the default config.xml which is without these tags. What led me to put it was the error in apk generation in voltbuilder that requested the sdk 32 because in my original project it was 23. But in no way can I consult the URL.

If I understood the second part of the question, when I run it in the Browser, the error does not occur.

I tried with sdk 31 but it didn’t work either. . tks.

Here is a link to android device remote debugging instructions: Remote debug Android devices - Chrome Developers

Give that a try and tell me what you see.

The errors below occur.

Thank you all for your help I managed to make it work, I found that the important thing is to use the tag below:

<plugin name="cordova-plugin-cleartext" source="npm" />

But I know that it’s not just him who needs it because I tried to put the config.xml with just her and it didn’t work.
Finally, the Config looked like this:

<?xml version="1.0" encoding="UTF-8"?>
<widget id = "com.nsbasic.app" version = "{version}"
xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:cdv="http://cordova.apache.org/ns/1.0">

<name>{title}</name>
<description>
{description}
</description>

<!-- Icons: Expand this section with sizes required for platforms you are targeting.
    Here is the list: http://wiki.phonegap.com/w/page/36905973/Icons%20and%20Splash%20Screens
-->
<icon src="{icon}" gap:role="default" width="57" height="57"/>
<preference name='SplashScreenDelay' value='2000' />
<preference name="AutoHideSplashScreen" value="true" />
<preference name='AndroidWindowSplashScreenAnimatedIcon' value='resources/splashTemplate.png' />
<preference name='AndroidWindowSplashScreenBackground' value='#000000' />
<plugin name='cordova-plugin-splashscreen' source='npm' />



<platforms>
  <platform name="android" >
    <preference name="AndroidXEnabled" value="true" />
    <preference name="AndroidPersistentFileLocation" value="Internal" />
    <preference name="android-minSdkVersion" value="22" />
    <preference name="android-targetSdkVersion" value="31" />
    <preference name="android-installLocation" value="auto" />
    <preference name="android-multiDexEnabled" value="true" />
    <preference name="Fullscreen" value="false" />
    <preference name="Orientation" value="portrait" />
    <preference name="android-signed" value="true" />
    <preference name="AndroidLaunchMode" value="singleTop" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-intent href="market:*" />
    <access origin="*" />
    <icon platform="android" src="package-assets/ic_ldpi.png" density="ldpi" width="36" height="36" />
    <icon platform="android" src="package-assets/ic_mdpi.png" density="mdpi" width="48" height="48" />
    <icon platform="android" src="package-assets/ic_hdpi.png" density="hdpi" width="72" height="72" />
    <icon platform="android" src="package-assets/ic_xhdpi.png" density="xhdpi" width="96" height="96" />
   </platform> 
</platforms>

<preference name="hostname" value="localhost" />
<preference name="AndroidInsecureFileModeEnabled" value="true" />

<!-- gap:plugin name="cordova-plugin-whitelist" source="npm" /> -->
<allow-navigation href="*" />
<access origin="*" />
<allow-intent href="*" />


<plugin name="cordova-plugin-statusbar" source="npm" />
  <preference name="StatusBarBackgroundColor" value="{phoneGapStatusBarColor}" />
  <preference name="StatusBarStyle" value="{phoneGapStatusBarStyle}" />


<plugin name="cordova-plugin-btprinter" source="npm" />

<plugin name="cordova-plugin-cleartext" source="npm" />

<plugin name="cordova-plugin-device" source="npm" />

<plugin name="cordova-plugin-androidx-adapter" source="npm" />




<!-- sample feature specification -->
<!-- <feature name="http://api.phonegap.com/1.0/network"/> -->

<!-- sample preference specification -->
<!-- <preference name="autorotate" value="false" readonly="true"/> -->
</widget>

I’ll try to find out exactly what you need in the config.xml and pass it on here.

That plugin looks a bit under-documented. However, I do see a stack overflow post that I suspect will help: Why am I seeing net::ERR_CLEARTEXT_NOT_PERMITTED errors after upgrading to Cordova Android 8? - Stack Overflow

I would suggest using HTTPS rather than HTTP. This is the proper solution to the problem. Even for testing there are options like https://ngrok.com/ to help with getting a proper certificate and your API on the internet.