Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中创建后,网络似乎无法立即工作_Android_Networking_Emulation - Fatal编程技术网

在Android中创建后,网络似乎无法立即工作

在Android中创建后,网络似乎无法立即工作,android,networking,emulation,Android,Networking,Emulation,我使用Android异步Http客户端 我需要在创建应用程序时检索一些数据。Internet权限已打开。但连接始终失败,并出现错误: java.io.IOException: UnknownHostException exception: Unable to resolve host "google.com": No address associated with hostname 起初,我认为问题在于DNS。我添加了-dns服务器8.8.8.8,8.8.4.4作为默认选项。但我还是犯了错

我使用Android异步Http客户端

我需要在创建应用程序时检索一些数据。Internet权限已打开。但连接始终失败,并出现错误:

java.io.IOException: 
UnknownHostException exception: 
Unable to resolve host "google.com": No address associated with hostname
起初,我认为问题在于DNS。我添加了
-dns服务器8.8.8.8,8.8.4.4
作为默认选项。但我还是犯了错误。我在模拟器中检查了浏览器应用程序,它工作正常。所以问题一定在我的代码中。我试着睡了一秒钟,这个问题解决了。显然,当我执行查询时,有些东西没有处于就绪状态

请帮我弄清楚这里发生了什么。先谢谢你

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //if I remove the sleep the http query fails
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://google.com/", new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
            System.out.println(statusCode);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
            System.out.println(e);
        }
    });
}

问题是,当模拟器启动时,internet连接没有立即打开。

尝试将“www.”添加到url的开头。我尝试了,但没有成功。您是否在清单中添加相关权限?我认为不是这样。但是是的,
ACCESS\u NETWORK\u STATE
INTERNET
都在那里。