error connection refused

前端 未结 5 872
隐瞒了意图╮
隐瞒了意图╮ 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:12

    For me problem solved by deleting proxyHost,proxyPort, etc from gradle.properties

    0 讨论(0)
  • 2020-11-30 02:21

    localhost would be the Android device itself. I assume that this is not where your servlet is. You'll need to enter the hostname or IP of wherever your servlet is.

    (If it's really on your device (why?!), then you need to make sure you have the INTERNET permission. You could try connecting to it from the built-in browser.)

    0 讨论(0)
  • 2020-11-30 02:22

    I had the same problem but I solved it by putting in the following label said

    <uses-permission android:name="android.permission.INTERNET" />
    

    which allowed me to connect to the Internet.

    0 讨论(0)
  • 2020-11-30 02:24

    The better is that you put your PC LAN's IP , for example , in windows , run "ipconfig" in a cmd console , suppose that your IP is : 192.168.1.34 , then

    HttpPost httpMethod = 
       new HttpPost("http://192.168.1.34:8080/getHeader/HeaderServlet");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题