为Nativescript移动应用本地开发Golang后端的技术问题咨询
Hey Tony, let's work through the common issues you might run into with your setup and how to fix them:
First off, using http://localhost:8080/ in your emulator is almost certainly going to fail—here's why, plus step-by-step fixes:
1. Fix the Localhost Mismatch
The emulator's localhost points to its own internal system, not your development machine. You need to use your machine's local network IP address instead:
- Find your IP: On Windows run
ipconfig, on Mac/Linux runifconfigorip addr(look for a line likeinet 192.168.x.x) - Update your request URL to something like:
http://192.168.1.105:8080/
2. Make Sure Your Golang Backend is Accessible
If your Go server is only listening on localhost, it won't accept connections from external devices (including emulators). Update your server code to listen on all network interfaces:
// Replace this: http.ListenAndServe("localhost:8080", nil) // With this: http.ListenAndServe(":8080", nil)
3. Actually Trigger the HTTP Request (Angular Fix)
Angular's http.get() returns an observable, which won't execute until you subscribe to it. Your current code doesn't send the request at all—fix it like this:
public get_test(){ this.http.get("http://YOUR_LOCAL_IP:8080/") .subscribe( (response) => { console.log("Success!", response.text()); // Handle your response data here }, (error) => { console.error("Request failed:", error); // Add error handling logic here } ); }
4. Double-Check Permissions (Android)
Nativescript usually adds internet permissions automatically, but it's worth verifying:
- Make sure
AndroidManifest.xmlincludes:<uses-permission android:name="android.permission.INTERNET"/>
For future deployment, if you're new to backend hosting, platforms like Railway or Fly.io are great for beginners—they let you deploy Go apps with just a few commands, no complex server setup needed.
内容的提问来源于stack exchange,提问作者Tony

