Monday, January 7, 2013

Connecting Android to Apache on localhost

Sometimes the requirements given to client/s are a bit odd especially when you are working as a freelance.  In my previous android projects, apps that requires internet permissions is as easy as what on the android documentation says.  Recently, I've got a project that uses internet to fetch updates.  The resources that I have are a site and a mysql database.  Now, I have to setup this site into local and run some tests on localhost.  In order for you to connect your android device to access localhost, make sure your laptop and android device are on the same wifi network.  You need to get the ip address of your localhost.  To do this, execute this command:

$ ifconfig

This is a utility used by system administrators to display and analyze network interface parameters.  Result should display like the image below.

The ip address of the laptop is at the wlan0 which is 192.168.1.35.  We may have different results depending on how many devices connected on wifi.  You may have something like 192.168.1.xx.  So on your test device, try changing the localhost to your ip address.

WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("http://192.168.1.35/drupal/");

Thanks to this post at StackOverflow.

9 comments:

  1. This blog explain the how to connect Android to Apache localhost that is very helpful for me during this type of experiment so thanks for posting this of information.

    Mobile App Development

    ReplyDelete
  2. This is obvious that Localhost refers to the device on which the code is running, in this case the emulator.

    Use 10.0.2.2 instead, read more: http://developer.android.com/tools/devices/emulator.html#networkaddresses

    ReplyDelete
  3. @Paresh thanks for that info :)

    ReplyDelete
  4. Hi Every one i have 1 prob can u plz help me...

    Am also facing the same problem am doing the work in android app's the CCTV to show the web application but code is every thing done but not displaying the result in web. when i was connected the "http://www.google.com" this will open in web but when i was connecting the (CCTV)"http://192.168.1.xx./...." like this way it's not showing any thing ,showing the white screen so can u plz help me ...

    ReplyDelete
  5. make sure that your device is connected on the same network as your wifi. in my case, its 192.168.1.35

    ReplyDelete
  6. Thanks for Response. Am sure both in same IP address...

    ReplyDelete
  7. Great insights and such a beautifully written article.

    Thanks for sharing it's highly appreciated.
    Mobile Application Development Company

    ReplyDelete
  8. Are you looking for a car rental app development solution for your business? Let's explore key features, development cost, tech stack, required team structure and with all the essential details

    ReplyDelete