Skip to content

Commit

Permalink
correctly resolve localhost as ipv4 on android
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas committed May 10, 2024
1 parent ac97bce commit 9c42705
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public JSONArray handleList(Uri uri, RequestHandler.ServerInfo serverInfo) throw
String includeInvalid=uri.getQueryParameter("invalid");
List<AddonInfo> addons=getAddons(!((includeInvalid != null) && (includeInvalid.toLowerCase().equals("true"))));
JSONArray rt=new JSONArray();
String host="localhost";
String host=AvnUtil.getLocalHost().getHostAddress();
if (serverInfo != null && serverInfo.address != null) {
host=serverInfo.address.getHostAddress();
}
Expand Down
10 changes: 2 additions & 8 deletions android/src/main/java/de/wellenvogel/avnav/appapi/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

import de.wellenvogel.avnav.main.R;
import de.wellenvogel.avnav.util.AvnLog;
import de.wellenvogel.avnav.util.AvnUtil;
import de.wellenvogel.avnav.worker.EditableParameter;
import de.wellenvogel.avnav.worker.GpsService;
import de.wellenvogel.avnav.worker.Worker;
Expand Down Expand Up @@ -423,14 +424,7 @@ public boolean isRunning(){
}

private InetAddress getLocalHost() throws UnknownHostException {
InetAddress local=null;
try {
local = Inet4Address.getByName("localhost");
}catch(Exception ex){
AvnLog.e("Exception getting localhost: "+ex);
}
if (local == null) local=Inet4Address.getLocalHost();
return local;
return AvnUtil.getLocalHost();
}

static class AvNavHttpServerConnection extends DefaultHttpServerConnection{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public void launchBrowser() {
if (!webServer.isRunning()) return;
int port = webServer.getPort();
if (port == 0) return;
String start = "http://localhost:" + port + "/viewer/avnav_viewer.html";
String start = "http://"+AvnUtil.getLocalHost().getHostAddress()+":" + port + "/viewer/avnav_viewer.html";
if (BuildConfig.DEBUG) start += "?log=1";
AvnLog.d(LOGPRFX, "start browser with " + start);
try {
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/de/wellenvogel/avnav/util/AvnUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ public static long toTimeStamp(net.sf.marineapi.nmea.util.Date date, net.sf.mari
public static InetAddress getLocalHost() throws UnknownHostException {
InetAddress local=null;
try {
local = InetAddress.getByName("localhost");
local = Inet4Address.getByName("localhost");
}catch(Exception ex){
AvnLog.e("Exception getting localhost: "+ex);
}
if (local == null) local=InetAddress.getLocalHost();
if (local == null) local=Inet4Address.getLocalHost();
return local;
}

Expand Down

0 comments on commit 9c42705

Please sign in to comment.