Maheera Jazi
Maheera Jazi

Reputation: 214

Access wamp server files via PC name insted of local IP address -Android application

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"; 

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"; 

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!

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

any help at all would be greatly appreciated.

Upvotes: 1

Views: 1629

Answers (1)

user1386321
user1386321

Reputation:

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) {

     }  

}

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.

Upvotes: 1

Related Questions