Android java.net.UnknownHostException: Host is unresolved

前端 未结 17 1036
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 08:43

This code doesn\'t work:

URL         url   = new URL( xmlPath );
InputSource input = new InputSource( url.openStream() );

all the time, res

17条回答
  •  萌比男神i
    2020-12-01 09:31

    In my case the problem was elsewhere. I had to:

    • add

    • setup a proxy server on my emulator : Emulator > settings > wireless and network > Mobile Networks > Access Point Name > "the access point you have ;)". Here you can set all the proxy port url and login/pass you want.

    AND

    • Call my network operation in a separate thread. The easy way was to implement an AsynchTask.

    I found the solution here:

    HTTP doesn't work in Android emulator

    " This exception is there for a reason. Network activity can take time, performing networking on the main thread, which is the same thread that responsible for updating the UI, would freeze the thread until the networking is done (this is what happens on every thread, but when it's performed on a dedicated thread, it's ok). In Android, if the UI-thread isn't active for 5 seconds, it will show the Application is not responsive, Do you want to close it? dialog. "

提交回复
热议问题