1

When I build a debug version of my app and tested it on my android phone, the API's I am using didn't connected to it but when I tested the HTML code itself (with the API's) on my pc (not in cordova, in my browser) it worked just fine. I am using cordova-android 10.1.1 and 30.0.3 as my android build tools sdk version. The API I am using is a simple weather API from WeatherApi.Com. I am using it by the fetch function in js and gets the output to a div. An example code :

<div id="weather">Loading...</div>
<script>
fetch("the api url")
.then((response) => {response.json()})
.then((data) => {
    document.getElementById("weather").innerHTML = data.weather;
})
</script>

With this code, the div stays on "Loading..." forever.

I am not using any plugin, and I just removed the css and the js from the default index.html file I got in the www folder.

Is there a solution for this issue?

I tried to build a debug version of my app and check if everything is ok with the API before building the release and everything was fine in my chrome browser, but when I tested it on my android mobile phone it didn't worked.

ron7097
  • 11
  • 2
  • Remove the `{}` wrapper in the `then` and change it to: `.then((response) => response.json())`. If it is working in the browser, this is not the code you are using – adiga Dec 20 '22 at 08:23
  • The wrapper does not metter, I know I can remove it. I'm sorry but I don't understand what you ment in "this is not the code you are using". – ron7097 Dec 20 '22 at 08:36
  • You should check the console and network logs (for example using chrome, connect your android phone to your computer with debug mode enabled, then type this address in chrome: `chrome://inspect/#devices`. You may need to refresh the webview to be able to see the requests in the network tab). Maybe the connection is refused or you have an error? If the case add more details to the question – Kaddath Dec 20 '22 at 08:36
  • I already checked the network and everything was fine in my pc and my phone. I also tested it on the phone itself (the single HTML) and everything was ok. Only in the cordova app it's not working. – ron7097 Dec 20 '22 at 08:42
  • Then did you try to add a log for each step to see if the data arrives to the first callback and to the second? – Kaddath Dec 20 '22 at 08:47
  • It should either be `.then((response) => { return response.json() })` or `.then((response) => response.json())`. The current code you have posted will throw an error in the `data.weather` line because `data` will be undefined – adiga Dec 20 '22 at 09:54
  • I noticed that, this is just an example code. If it was working, it would return "undefined" in the div and then I will fix that, but it not even showing this, just the "Loading..." text I set inside the div. – ron7097 Dec 20 '22 at 11:04

0 Answers0