java.net.UnknownHostException:无法解析主机“<url>”:没有与主机名相关的地址和字符 0 处的输入结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19951466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.UnknownHostException: Unable to resolve host "<url>": No address associated with hostname and End of input at character 0 of
提问by Reshma
I've created an app that loads a question from my web services, and it works fine. But, sometimes it crashes and I do not get the reason why this is happening, especially because I have also given it the required permissions. It works fine, but at random, it crashes and gives me this report.
我创建了一个从我的网络服务加载问题的应用程序,它运行良好。但是,有时它会崩溃并且我不明白为什么会发生这种情况,尤其是因为我还为其授予了所需的权限。它工作正常,但随机崩溃并给我这个报告。
private void sendContinentQuestions(int id) {
// TODO Auto-generated method stub
//Get the data (see above)
JSONArray json = getJSONfromURL(id);
try{
for(int i=0; i < json.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jObject = json.getJSONObject(i);
longitude":"72.5660200"
String category_id = jObject.getString("category_id");
String question_id = jObject.getString("question_id");
String question_name = jObject.getString("question_name");
String latitude = jObject.getString("latitude");
String longitude = jObject.getString("longitude");
String answer = jObject.getString("answer");
String ansLatLng = latitude+"|"+longitude ;
Log.v("## data:: ###",question_id+"--"+question_name+"-cat id-"+category_id+"--ansLatLng "+ansLatLng+" answer: "+answer);
all_question.add(new QuestionData(game_id,category_id,question_id,question_name,ansLatLng,answer));
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
public JSONArray getJSONfromURL(int id){
String response = "";
URL url;
try {
url = new URL(Consts.GET_URL+"index.php/Api/getQuestion?cat_id="+id);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
InputStream is = http.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
response = br.readLine();
Log.v("###Response :: ###",response);
http.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//try parse the string to a JSON object
JSONArray jArray = null;
try{
jArray = new JSONArray(response);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
11-13 15:02:52.307: W/System.err(8012): java.net.UnknownHostException: Unable to resolve host "www.xyz.com": No address associated with hostname
11-13 15:02:52.317: W/System.err(8012): at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
11-13 15:02:52.317: W/System.err(8012): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-13 15:02:52.317: W/System.err(8012): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
11-13 15:02:52.317: W/System.err(8012): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
11-13 15:02:52.327: W/System.err(8012): at com.abc.xyz.ContinentActivity.getJSONfromURL(ContinentActivity.java:400)
11-13 15:02:52.327: W/System.err(8012): at com.abc.xyz.ContinentActivity.sendContinentQuestions(ContinentActivity.java:327)
11-13 15:02:52.327: W/System.err(8012): at com.abc.xyz.ContinentActivity.access(ContinentActivity.java:323)
11-13 15:02:52.327: W/System.err(8012): at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:254)
11-13 15:02:52.327: W/System.err(8012): at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:1)
11-13 15:02:52.327: W/System.err(8012): at android.os.AsyncTask.call(AsyncTask.java:287)
11-13 15:02:52.327: W/System.err(8012): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-13 15:02:52.327: W/System.err(8012): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
11-13 15:02:52.327: W/System.err(8012): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-13 15:02:52.337: W/System.err(8012): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-13 15:02:52.337: W/System.err(8012): at java.lang.Thread.run(Thread.java:841)
11-13 15:02:52.337: W/System.err(8012): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
11-13 15:02:52.337: W/System.err(8012): at libcore.io.Posix.getaddrinfo(Native Method)
11-13 15:02:52.337: W/System.err(8012): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
11-13 15:02:52.337: W/System.err(8012): at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
11-13 15:02:52.337: W/System.err(8012): ... 24 more
11-13 15:02:52.337: E/log_tag(8012): Error parsing data org.json.JSONException: End of input at character 0 of
11-13 15:02:52.337: W/dalvikvm(8012): threadid=194: thread exiting with uncaught exception (group=0x417c1700)
11-13 15:02:52.337: E/AndroidRuntime(8012): FATAL EXCEPTION: AsyncTask #5
11-13 15:02:52.337: E/AndroidRuntime(8012): java.lang.RuntimeException: An error occured while executing doInBackground()
11-13 15:02:52.337: E/AndroidRuntime(8012): at android.os.AsyncTask.done(AsyncTask.java:299)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-13 15:02:52.337: E/AndroidRuntime(8012): at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.lang.Thread.run(Thread.java:841)
11-13 15:02:52.337: E/AndroidRuntime(8012): Caused by: java.lang.NullPointerException
11-13 15:02:52.337: E/AndroidRuntime(8012): at com.abc.xyz.ContinentActivity.sendContinentQuestions(ContinentActivity.java:328)
11-13 15:02:52.337: E/AndroidRuntime(8012): at com.abc.xyz.ContinentActivity.access(ContinentActivity.java:323)
11-13 15:02:52.337: E/AndroidRuntime(8012): at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:254)
11-13 15:02:52.337: E/AndroidRuntime(8012): at com.abc.xyz.ContinentActivity$LoadQuestions.doInBackground(ContinentActivity.java:1)
11-13 15:02:52.337: E/AndroidRuntime(8012): at android.os.AsyncTask.call(AsyncTask.java:287)
11-13 15:02:52.337: E/AndroidRuntime(8012): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
采纳答案by RxRead
I encountered this problem too, reconnecting the WiFi can solve this.
我也遇到了这个问题,重新连接WiFi可以解决这个问题。
For us ,we can check if the phone can resolve the host to IP when we start application. If it cannot resolve, tell the user to check the WiFi and then exit.
对于我们来说,我们可以在启动应用程序时检查手机是否可以将主机解析为 IP。如果不能解决,告诉用户检查WiFi,然后退出。
I hope it helps.
我希望它有帮助。
回答by adubey
I got the same error and the issue was that I was on VPN and I didn't realize it. After disconnecting from the VPN and reconnecting to my WIFI network, the problem was resolved.
我遇到了同样的错误,问题是我在使用 VPN 而我没有意识到这一点。断开 VPN 连接并重新连接到我的 WIFI 网络后,问题解决了。
回答by Ajay Takur
Missed to configure tag in manifest file
错过在清单文件中配置标签
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
回答by CMash
I had the same exception in the simulator (Android Studio on OSX) but connecting to the same URL on the iOS simulator worked fine... Looks like it all stemmed from the fact I'd be running the simulator whilst connected to a personal hotspot for my internet connection and then came back later while connected to wifi and the simulator didn't like the new internet connection for some reason, seems like it thought the old hotspot was the current connection, which was no longer working..
我在模拟器中遇到了同样的异常(OSX 上的 Android Studio),但是在 iOS 模拟器上连接到相同的 URL 工作正常......看起来这一切都源于我在连接到个人热点的同时运行模拟器的事实用于我的互联网连接,然后稍后在连接到 wifi 时回来,并且模拟器由于某种原因不喜欢新的互联网连接,似乎它认为旧热点是当前连接,它不再工作..
Closing and relaunching the simulator worked!
关闭并重新启动模拟器工作!
回答by Jiyeh
If reconnecting the WiFi doesn't work for you, try reboot your device.
如果重新连接 WiFi 对您不起作用,请尝试重新启动您的设备。
This works for me. Hope it helps.
这对我有用。希望能帮助到你。
回答by A. Sullivan
I encountered this error when running my Android app on my home WiFi, then trying to run it on different WiFi without closing my simulator.
在我的家庭 WiFi 上运行我的 Android 应用程序时,我遇到了这个错误,然后尝试在不同的 WiFi 上运行它而不关闭我的模拟器。
Simply closing the simulator and re-launching the app worked for me!
只需关闭模拟器并重新启动应用程序即可!
回答by Hernán Daniel Pi?a Landinez
I was testing my application with the Android emulator and I solved this by turning off and turning on the Wi-Fi on the Android emulator device! It worked perfectly.
我正在使用 Android 模拟器测试我的应用程序,我通过关闭和打开 Android 模拟器设备上的 Wi-Fi 解决了这个问题!它工作得很好。
回答by Ashandra Singh
I had the same problem. java.net.UnknownHostException: Unable to resolve host “”...
我有同样的问题。java.net.UnknownHostException: 无法解析主机“”...
I'm running Visual Studio 2019 and Xamarin.
我正在运行 Visual Studio 2019 和 Xamarin。
I also switched back to my WiFi but was on a hot spot.
我也切换回我的 WiFi,但在热点上。
I solved this by clean swiping the emulator. Restore to factory settings. Then re-running visual studio xamarin app which wil redeploy your app again to the fresh emulator.
我通过干净地滑动模拟器解决了这个问题。恢复出厂设置。然后重新运行 Visual Studio xamarin 应用程序,它将再次将您的应用程序重新部署到新的模拟器。
It worked. I thought I was going to battle for days to solve this. Luckily this post pointed me in the right direction.
有效。我以为我要战斗几天才能解决这个问题。幸运的是,这篇文章为我指明了正确的方向。
I could not understand how it worked perfectly before and then stopped with no code change.
我无法理解它之前是如何完美运行的,然后没有更改代码就停止了。
This is my code for reference:
这是我的参考代码:
using var response = await httpClient.GetAsync(sb.ToString());
string apiResponse = await response.Content.ReadAsStringAsync();
回答by Gabriel Guedes
I was having the same issue, but with Glide. When I was going to disconnect from wifi and reconnect (just like it was suggested here), I noticed that I was in Airplane mode ?♂?
我遇到了同样的问题,但使用 Glide。当我要断开 wifi 连接并重新连接时(就像这里建议的那样),我注意到我处于飞行模式?♂?
回答by Aby Mathew
I had this issue on Android 10,
我在 Android 10 上遇到过这个问题,
Changed targetSdkVersion 29
to targetSdkVersion 28
issue resolved. Not sure what is the actual problem.
更改targetSdkVersion 29
为targetSdkVersion 28
问题已解决。不确定实际问题是什么。
I think not a good practice, but it worked.
我认为这不是一个好习惯,但它奏效了。
before:
前:
compileSdkVersion 29
compileSdkVersion 29
minSdkVersion 14
minSdkVersion 14
targetSdkVersion 29
targetSdkVersion 29
Now:
现在:
compileSdkVersion 29
compileSdkVersion 29
minSdkVersion 14
minSdkVersion 14
targetSdkVersion 28
targetSdkVersion 28