通过本地 IP 地址的 PC 名称访问 wamp 服务器文件-Android 应用程序 [英] Access wamp server files via PC name insted of local IP address -Android application

查看:39
本文介绍了通过本地 IP 地址的 PC 名称访问 wamp 服务器文件-Android 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用程序中,我希望能够通过 PC 名称(主机名)而不是 PC 本地 IP 地址访问 wamp 服务器文件,这可能吗?导致用户需要在 Android 应用程序中更改本地 IP 地址.每次都需要更新或从 MySQL 数据库中获取数据这是一件烦人的事情!

In my Android application, I wanna be able to access the wamp server files via the PC name (hostname) not PC local IP address, Is it that possible? cause the user need to change the local IP address in Android app. each time needs to update or get data from MySQL database which annoying thing!

// url to get all items list
 private static String url_all_items="http://Local IP Address/android_connect/test.php"; 

但是我需要能够将 PC 名称而不是本地 IP 地址发送为:

But what I need to be able to send PC name not local IP address as:

// URL to get all items list
 private static String url_all_items="http://PC Name/android_connect/test.php"; 

实际上,我尝试发送 PC 名称而不是本地 IP 地址,但这不起作用,我尝试了很多解决方案,在 wamp 服务器配置中,在 Windows 文件上的主机上等.但这也不起作用!是否有任何其他配置能够通过 android 应用程序而不是 IP 地址发送 PC 名称?!虽然当我发送本地 IP 地址时它运行良好!

Actually, I tried to send the PC name instead of Local IP address but this does not work, I tried many solutions, In wamp server configurations, on hosts on windows file, etc. BUT also this not work! Is there any additional configurations to be able to send PC name via the android app instead off IP address?! although when I send local IP address it works well!

总而言之,有什么办法可以使用PC名称而不是本地IP地址访问WAMP服务器?

In sum, Is there any way that I can access the WAMP server using the PC name instead of the local IP address?

非常感谢任何帮助.

推荐答案

解决问题的简单方法是无需在 wamp 服务器或 Windows 中进行任何配置,但解决方案在于代码本身.正如我所见,您不希望 android 应用程序用户放置或使用 IP,您只允许用户放置服务器名称.因此,最好的方法是获取输入的服务器名称的 IP 地址.获得服务器名称的IP地址后,可以使用返回的IP重建所需的URL.以下代码显示了一段有助于您实现的代码.

The simple way to solve your problem is without doing any configuration in the wamp server or in the windows, however the solution lies in the code itself. As I see, you don't want the android app user to put or use IP, you just allow the user to put the sever name only. Thus, the best way to do that is by getting the IP address of the entered server name. On the IP address of server name is obtained, you can rebuild the needed URLs using the returned IP. The following code shows a piece of code that helps you in the implementation.

private static String url_all_items="http://%s/android_connect/test.php";
private static void ResolveURL() {

     try {
           InetAddress addr = InetAddress.getByName("mahdi-pc");
           String host = addr.getHostAddress();

           url_all_items=String.format(url_all_items, host);
           System.out.println(url_all_items);

     } catch (UnknownHostException ex) {

     }  

}

使用上面的代码,很明显,这种情况下的服务器名称是mahdi-pc",返回的IP地址将是它的.请注意,此代码仅对本地网络地址有效.之后,返回的 IP 地址在 url_all_items 链接中使用字符串格式替换.链接格式正确后,您可以使用 IP 地址建立连接,而无需在 URL 中使用服务器名称.

Using the above code, it is clear that the sever name in this case is named "mahdi-pc" and the returned IP address will be for it. Note that this code is valid only for local network addresses. After that, the returned IP address is replaced in url_all_items link using string format. Once the link is formatted correctly , you can establish your connection with using the IP address without using the name of the server in the URL.

我希望该解决方案对您有用.

I hope that the solution works fine with you.

这篇关于通过本地 IP 地址的 PC 名称访问 wamp 服务器文件-Android 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆