Search code examples
androidapachehttprequest

I can't access my Apache server from my Android device


it is my second time asking a question on stackoverflow. I'm trying to access to my local apache server that I've set up on my desktop computer (ip that starts with ie. 192.168.1*.**)on my android phone. but somehow it does not allow me to access to the server at all! :( strangely, sometimes it does allow me to access to the server lol! but it's only one out of 20 for each attempts. Also, it doesn't allow me to access to the server on my laptop either.. I really need some help guys.. it was working fine when i was working locally within my desktop pc by using emulator. I've done quite a lot of research about it but i wasn't able to find any solutions.

Thank you guys

=====================================

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

HttpClient client = new DefaultHttpClient(setHttpParams());
HttpPost request = new HttpPost(PATH + FILENAME);

if (json != null) {

        request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
        request.setHeader("json", json.toString());

} else {


        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}



HttpResponse response = client.execute(request);

so that is the code, error occurs when it tries to execute the request. request includes the apache server ip PATH (192.168.**) + FILE_NAME(ie. index.php). I know why it causes the error because i cannot access to the 192.168.* server and read the desired file. I need help with this =(


Solution

  • For the computer running the webserver:

    • Make sure that your personal firewall rules are not preventing access to the web server.
    • For your phone to access the webserver over the air the server needs to be accessible from the web. It could be that your machine is behind a corporate firewall which is preventing access to it from external sources.
    • If you simply want to access it from the phone, make sure that both the webserver and phone are on the same network i.e. they have IP addresses from the same address range. You can do that by connecting your machine to wireless AP (either via ethernet or wireless) and making sure that your phone is connected to the same wireless AP.

    Moreover I just read your question again and noticed the line

    it does not allow access to to the server on my laptop either

    If you cannot access the webserver locally on the machine it is runnning on then there is something wrong in the way you configured your web server. So first make sure that your web server is running and configured properly. Open a browser and go to http://localhost or http://192.168.x.x if everything is configured right you will be able to access your web pages.

    Basically this is networking 101, if it is still giving you problem post some details such as what webserver, what port etc.

    If you are new to networking concepts and don't know how to configure a web server say Apache just Google how to configure Apache or use something called XAMPP which is a developer friendly bundle containing Apache, MySQL, PHP, FileZilla etc. Note that XAMPP shall never be used in a production environment as its default configuration is done in a way to make it easier to install, configure and run above mentioned packages for new users.