Unable to contact host machine (10.0.2.2) from emulator - android

I have a python REST API server running on my laptop. I am trying to write a rest client in Android (using Eclipse ADT etc) to contact it using Apache's client (org.apache.http.client) libraries.
The code is really simple, and basically does the following -
HttpGet httpget = new HttpGet(new URI("http://10.0.2.2:8000/user?username=tim"));
HttpResponse response = httpclient.execute(httpget);
However at execute, it exceptions out with a time out exception. I cannot hit the URL even from the browser in the emulator.
Details of the exception
org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:8000 timed out
However, I tried using the cREST client on Chrome on my laptop, and I am able to query the REST server fine.

Is it possible the machine is not on your network? Ie - it is on the other side of a router, on the internet somewhere? Because addresses starting "10." are reserved as private addresses and not routable.
See http://en.wikipedia.org/wiki/Private_network for more info

I had the same issue and here's how I figured out the cause:
Quit you emulator. Start your local server. In the browser, you should be able to access "http://localhost:8000/user?username=tim" and get some response. If you get a timeout, your server is likely not running. In my case, my python server had a break-point set and it was stuck there. Once I let it run, I was able to see responses on the browser and subsequently in the emulator (using 10.0.2.2).

Related

Ionic $http request get not working with error data null status -1

I use Ionic and Cordova framework for developing a mobile app, I've got the issue that can not use http get to request some url. For $http.get to some url that works fine, but some others not work and it response error like as
I/Web Console( 9179): {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[n ull],"url":"http://someexmaple.com/","headers":{"Accept":"application/json, text/plain, */*"},"withCredentials":true}," statusText":""}
This issue only happens when I test on real mobile device and using adb debugging tool. But I tested on Browser and Genymotion virtual devices, both of them worked well for all url request.
So I've know idea with this response error. I think may be my device got problem.
Finally I've resolved my problem. I use inspect tool in Chrome, and move to network tab for watching the network packet, I see when I access to the url of site, it redirect me to another server port (instead of 80, it redirect to 443), I've noticed this because the 'Remote Address field' is the same address of url server, but port was changed. So I changed my $http.get request url from port 80 into port 443. And then it works, so I guess that when $http.get to the url but it didn't redirect to another port to get data, so it just response the null.

Android connectivity/network configuration

I have a WebService running in a remote VM inside my company's network.
I can see the JSON response using Chrome on my laptop by typing the url in the address bar (http://VMaddress/servicename/).
However, I cannot do this using the Android emulator. My company uses a proxy and I have already defined it in both the emulator launch window and inside the Android application itself. I can connect to Google and stuff, but I still cannot connect to the remote VM.
So my question is:
How is networking handled in the Android emulator? I know it defines a different ip for localhost, but shouldn't I be able to at least see the JSON response in the emulator browser (since I defined the proxy that would allow me to access the Internet)?
Doesn't the emulator run on my laptop's connection?
I get a 403 forbidden response using both the emulator browser and the Android application.
I also tried another WebService URL (http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo) and I get a 200 response code, so my application is correctly accessing the Internet).
thanks in advance
---EDIT---
I snooped around in my connection settings in my laptop and going into the Proxy Settings in Chrome I had an exception in the proxy for the VM where my server is.
Removing that exception gives me the same 403 forbidden error I have in my emulator.
How can I apply the same exception to the emulator?
Somehow I managed to "resolve" this.
I am not entirely sure this is the reason, but I followed these steps. The emulator must start while there is no network available.
I also have the DNS servers from my company set in the Launcher menu using the options:
-dns-server server1,server2 (got the values from ipconfig /all in a command prompt). It will not work for me without these settings.
and I configured the proxy manually in the emulator by creating a new APN following these steps.

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 =(
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.

Bad request when connecting to ASP.NET page on IIS from android application

I have a ASP.NET website deployed to IIS with a couple of ashx that returns JSONs to be consumed by an Android application.
I have implemented an authentication logic using Basic Authentication.
The problem: When accessed from Android, the server response is a 400 Bad request. The httperr log file says "400 - Hostname -".
It works when I try it out on localhost from Android emulator
It works when accessing the ashx file on the server from a browser
It works when replicating the call in Fiddler
(If I use Fiddler with the Android Emulator, the Response will be -1 (and looking in Fiddler at the raw data sent, it seems to loose the host from the url) - but this is another issue so don't dwell on that, i just thought I would mention it...)
Turns out my problem (and solution) was the same as in this thread:
HTTP POST request with authorization on android

Android Emulator not connecting with localhost Apache

I am developing an app in Android, and the emulator is not connecting to my localhost.
I have researched the issue and found some ideas but nothing seems to be working.
Below is the code i have been using:,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/YES_BUTTON.php");
httpclient.execute(httpPost);
I have also tried a connection using "http://10.0.2.2:3128/YES_BUTTON.php";
The code would run a php file, but nothing happens, I have also checked the server access log and no connection was ever made with the Apache server from the emulator.
This is my first time building an app, and was wondering if there was something I haven't done, do I need to map the emulator port with the 3128 port on Apache, disable the firewall? I am developing the app on a laptop,with the Apache Server on the laptop also, so everything is being done on the same machine. Is there anything I havent done that needs to be done?
I bet you forgot to add the INTERNET permission :)
INTERNET

Categories

Resources