Use 10.0.2.2
instead of localhost
.
If you are referring to a localhost
from your device than use the http://10.0.2.2/
instead of the http://127.0.0.1/
or http://localhost/
.
Because your Android emulator is running on a Virtual Machine(QEMU)
and you can not connect to a server directly running on your PC.
So your code snippet will be like this:
HttpPost httpMethod = new HttpPost("http://10.0.2.2:8080/getHeader/HeaderServlet");
Refer this : Emulator Networking for more information.