error connection refused

前端 未结 5 892
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 01:37

I want to make an Http Connection to my own servlet. Here is my code:

try
{
    HttpClient client = new DefaultHttpClient();
    HttpPost httpMethod = new Ht         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 02:27

    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.

提交回复
热议问题