java.net.UnknownHostException:无法解析主机“ “:没有与主机名关联的地址和字符0处的输入结束

问题描述 投票:47回答:7

我创建了一个从我的Web服务加载问题的应用程序,它运行正常。但是,有时它崩溃,我不明白为什么会发生这种情况,特别是因为我还给了它所需的权限。它工作正常,但随机,它崩溃,并给我这个报告。

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$2(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$2.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$1.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$3.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$1.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$2(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$2.call(AsyncTask.java:287)
11-13 15:02:52.337: E/AndroidRuntime(8012):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
java android web-services android-asynctask
7个回答
150
投票

我也遇到了这个问题,重新连接WiFi可以解决这个问题。

对我们来说,我们可以在开始申请时检查手机是否可以将主机解析为IP。如果无法解决,请告诉用户检查WiFi然后退出。

我希望它有所帮助。


8
投票

我在模拟器(OSX上的Android Studio)中遇到了相同的异常,但在iOS模拟器上连接到相同的URL工作得很好......看起来这一切都源于我在连接到个人热点时运行模拟器的事实对于我的互联网连接,然后在连接到wifi时回来,模拟器由于某种原因不喜欢新的互联网连接,似乎它认为旧的热点是当前的连接,它不再工作..

关闭并重新启动模拟器工作!


6
投票

如果重新连接WiFi不适合您,请尝试重启设备。

这适合我。希望能帮助到你。


5
投票

我得到了同样的错误,问题是我在VPN上,我没有意识到这一点。断开VPN连接并重新连接到我的WIFI网络后,问题得以解决。


3
投票

错过了在清单文件中配置标记

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

2
投票

我在家庭WiFi上运行我的Android应用程序时遇到此错误,然后尝试在不关闭我的模拟器的情况下在不同的WiFi上运行它。

只需关闭模拟器并重新启动应用程序就适合我!


0
投票

我正在使用Android模拟器测试我的应用程序,我通过关闭并打开Android模拟器设备上的Wi-Fi解决了这个问题!它工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.