Calling url error in android

82 views
Skip to first unread message

Todd Johnson

unread,
Nov 2, 2025, 8:16:02 AMNov 2
to mementodatabase
I am getting an error only in android and I believe it has to do with an error " android.os.NetworkOnMainThreadException"  I have a location field that is added from the map manually. then later I try to add the location to another field in the same library in lat/long coordinates. It is failing on the call to the url but the url looks fine.  similear calls seem to work in other libraries and I am thinking it is related to permissions but I have checked all permissions and can see no difference between libraries. The same script works fine in desktop.
Here is the actual script: the read line is where the fail is occuring

// Get the address from LocationOnMap field
var address = entry().field("LocationOnMap").address;
//message("Address to geocode: " + address);
entry().set("debug1", address);
// Check if address field has content
if (address && address !== "") {
    // Encode the address for URL
    var encodedAddress = encodeURIComponent(address);
    entry().set("debug2", encodedAddress);
    // Build the Google Geocoding API URL
    var apiKey = "*";
    var url = "https:/" + "/" + "maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + "&key=" + apiKey;
   
    // Store the URL in the "URL called" field
    entry().set("URL called", url);
    message("DEBUG: URL being called: " + url);
   
    message("DEBUG: About to make HTTP request...");
   

        // Make the request to Google Geocoding API
        var result = http().get(url);
       // entry().set("debug2", result);
       // message("DEBUG: HTTP request completed");
        //message("DEBUG: HTTP response status: " + result.status);
       // message("DEBUG: Raw response body: " + result.body);
       
       
            var data = JSON.parse(result.body);
           
          //  message("DEBUG: Parsed data status: " + data.status);
          //  message("DEBUG: Number of results: " + (data.results ? data.results.length : "undefined"));
           
            // Check if geocoding was successful
            if (data.status === "OK" && data.results.length > 0) {
               // message("DEBUG: Geocoding successful, processing first result...");
                var location = data.results[0].geometry.location;
                var latitude = location.lat;
                var longitude = location.lng;
                var formattedAddress = data.results[0].formatted_address;
               // message("DEBUG: Latitude: " + latitude + ", Longitude: " + longitude);
               // message("DEBUG: Formatted Address: " + formattedAddress);  
               
                // Create location object for Memento Database
                var locationData = (latitude + "," + longitude);
                entry().set("Location", locationData);
               
            }
        }
               
               

Er Mo

unread,
Nov 2, 2025, 9:02:08 AMNov 2
to mementodatabase
Hallo
Ich glaube das hat nichts mit den GPS Daten zutun . Du willst ( mit der Roten Zeille ) eine String in das Feld " URL called " schreiben . Was ist das für ein Feldtyp ? 

Hello

I don't think this has anything to do with the GPS data. You want to write a string into the "URL called" field (using the red line). What type of field is that?

Ernst

Todd Johnson

unread,
Nov 2, 2025, 12:21:28 PMNov 2
to mementodatabase
My extreme apologies,  in my hurry to get that posted before getting up from the computer I indicated the wrong line as the problem.  Again apologies! It is the line that calls the http().get(url) where the problem occurs.  I have a version  using try{} and that is where I could see the error  android.os.NetworkOnMainThreadException. When I get the contents of the url that is called but throws the exception it will actually work in the browser on the desktop so I think that part is correct. 
Thank You, sorry again.

Er Mo

unread,
Nov 2, 2025, 12:29:27 PMNov 2
to mementodatabase
Hallo
OK . Hast du der Bibliothek erlaubt auf das Internet zuzugreifen . Hatt dein Handy auch Internet zugang ?

Hello

OK. Have you allowed the library to access the internet? Does your phone also have internet access?

Ernst

Todd Johnson

unread,
Nov 3, 2025, 6:29:33 AMNov 3
to mementodatabase
I don't know why but it appears to alter the line that I'm highlighting in red.  I'm pretty sure I tried to highlight this line:
var result = http().get(url); 
I have noticed my wording also gets altered in the translation too.
Sorry

Todd Johnson

unread,
Nov 3, 2025, 9:30:03 AMNov 3
to mementodatabase
Okay I've made another trigger inside the same  library. Everything works on desktop. It does not on my android.  I have gone into the permissions for the triggers and made sure network and library permissions are ok. I've looked for any other settings I can find that would prohibit the call to google geolocation. I have other triggers that go out and get the location, it is inside of a function. It doesn't have the same problem.  I feel like it could have something to do with being in the main thread for android, whatever that is.  I wonder if it has to do with the need to be asynchronous which I only understand to the point that it wouldn't hold up the main thread.
It's quite a stumping question for me right now.  I don't know what I have to change in the android version to work but it would be nice to know.  
Thanks, Todd

The problem occurs at:  var result = http().get(url);

// Get the address from LocationOnMap field
var address = entry().field("LocationOnMap").address;
message("Address to geocode: " + address);
entry().set("debug1", address);

// Check if address field has content
if (address && address !== "") {
    // Encode the address for URL
    var encodedAddress = encodeURIComponent(address);
    entry().set("debug2", encodedAddress);
    // Build the Google Geocoding API URL
    var apiKey = "******************";
    var url = "https:/" + "/" + "maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + "&key=" + apiKey;
   
    // Store the URL in the "URL called" field
    entry().set("URL called", url);
    //message("DEBUG: URL being called: " + url);
   
    //message("DEBUG: About to make HTTP request...");
   
    // Make the request to Google Geocoding API
    var result = http().get(url);
    var data = JSON.parse(result.body);
   
   message("result is " + data.results[0].geometry.location.lat);
   entry().set("debug3", data.results[0].geometry.location.lat);

Todd Johnson

unread,
Nov 3, 2025, 9:31:18 AMNov 3
to mementodatabase
It says the library has permission to access the internet.  Phone has internet access.

Er Mo

unread,
Nov 3, 2025, 12:56:21 PMNov 3
to mementodatabase
Hallo
Eine Möglichkeit wäre das du für Google nicht die Berechtigung hast . Eine Abfrage würde dann auch fehlschlagen . Versuche die URL direckt in Handybrauser eintzgeben , was der dann Sagt .

Hello
One possibility is that you don't have permission to access Google. A query would then fail. Try entering the URL directly into your mobile browser and see what it says.

Ernst

Todd Johnson

unread,
Nov 10, 2025, 5:25:15 AM (9 days ago) Nov 10
to mementodatabase
In case anyone has the same problem, and I think I actually had a similiar problem before but didn't catch this again but:

In at least my android Samsung Galaxy S23 Ultra, it will not allow the geolocation search for a trigger activated before saving the entry.  It has to be triggered after the save!
Reply all
Reply to author
Forward
0 new messages