title: Android - Locate IP of WAMP Server on a Local Netwiok
body: <p>Can someone help me? I'm currently doing a <strong>Android Application</strong> project. </p>
  
  <p>But it was too late for me to know that <strong>MYSQL</strong> is not entirely compatible to <strong>Android</strong>. </p>
  
  <p>I can't go back now. As the title said that I am using a <strong>WAMP Server</strong> to host my <strong>MYSQL Database</strong> on the Local netwok from a computer. </p>
  
  <p>And as I read (<em>Correct me If I'm wrong</em>) that you need the actual <strong>IP</strong> of that computer to grab the data from the <strong>WAMP Server</strong>. </p>
  
  <p>If at any given moment that the <strong>IP</strong> of the <strong>Hosting Computer</strong> changes, the <strong>Android Application</strong> won't be able to connect to the <strong>MYSQL Database</strong>.</p>
  
  <blockquote>
    <p>Is there anyway for you to get the <strong>IP</strong> of the <strong>Hosting Computer</strong> programtically?</p>
    
    <p>I tried by using the current <strong>IP Address</strong> of the device to get the <strong>Network IP</strong>.</p>
  </blockquote>
  
  <p><strong>Example:</strong> </p>
  
  <p><strong>Device IP</strong> is <strong>192.168.1.2</strong> and <strong>Hosting Computer IP</strong> is <strong>192.168.20</strong>, now we know that the <strong>Network IP</strong> is <strong>192.168.1.1</strong>. Then will loop from <strong>192.168.1.2</strong> until <strong>192.168.1.254</strong>, the loop will only break/stop if it got the response you needed. Then saves that <strong>IP</strong> to string.</p>
  
  <pre><code>public String isConnectedToServer(String url, int index) {
      StrictMode.ThreadPolicy policy = new
              StrictMode.ThreadPolicy.Builder()
              .permitAll().build();
      StrictMode.setThreadPolicy(policy);
  
      String result = "";
      for(int octet = 2; octet &lt;= 254; octet++) {
          String compare = ""+octet;
          if(!compare.equals(url.substring(index + 1, url.length()))) {
              try {
                  HttpURLConnection.setFollowRedirects(false);
                  HttpURLConnection con = (HttpURLConnection) new URL("http://" + url.substring(0, index + 1) + "" + octet + "/AVMSSv2").openConnection();
                  con.setRequestMethod("HEAD");
                  con.setRequestProperty("User-Agent", "Android Application");
                  con.setRequestProperty("Connection", "close");
  
                  //Toast.makeText(LogIn.this, "NETWORK CONNECTED", Toast.LENGTH_LONG).show();
                  if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                      result = "http://" + url.substring(0, index + 1) + "" + octet + "/AVMSSv2/pages/android.php";
                      break;
                  } else {
                      result = "";
                  }
                  Log.w("APP", "Network");
              } catch (Exception e) {
                  // Handle your exceptions
                  //Toast.makeText(LogIn.this, "NETWORK ERROR CONNECTION", Toast.LENGTH_LONG).show();
                  Log.e("APP", "Exception", e);
                  result = "";
              }
          }
      }
      return result;
  }
  </code></pre>
  
  <blockquote>
    <p>The above code is just an example of mine but does not seem to work. And I already Used <strong>AsyncTask</strong>. Feel free to give me any suggestion.</p>
    
    <p><strong>P.S.</strong> The problem is that I can't change to a different database. Because the current project is Multi-Platform. That's why I need to stick to MYSQL because this is what the other platform are currently Using.</p>
    
    <p>And I wish to stick to a <strong>Dynamic Host of IP</strong> as possible.</p>
  </blockquote>