无法创建套接字。的UnknownHostException

时间:2013-08-16 18:40:23

标签: android

我正在开发Java Server和Android客户端的简单程序,当我尝试将我的android客户端连接到java服务器时,它总是给出UnknownHostException,我不是在实际设备上使用模拟器测试整个程序。

Android客户端

public void testWifi(){
    String serv= Context.WIFI_SERVICE;
            WifiManager wifi = (WifiManager)getSystemService(serv);

           if(!wifi.isWifiEnabled())
           {
                Intent btIntent= new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);
        startActivityForResult(btIntent,1);
       }

        boolean isWifi;
        NetworkInfo cur;
        Context con;
        ConnectivityManager conManager;
    do
    {
            con=getApplicationContext();
    conManager = ConnectivityManager)con.getSystemService(Context.CONNECTIVITY_SERVICE);
    cur = conManager.getActiveNetworkInfo();
    isWifi = (cur != null);
    try
    {
        Thread.sleep(2000);
    }
    catch(Exception ex){}
    }while(!isWifi);

    String address = getIpAddress();
    String toastText = "Connection is esablished to" + address; 
    Toast t = Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG);
    t.show();
    TextView ss;
    try
    {

        Socket s = new Socket(address,5000);//What i am doing wrong here.?
        InputStreamReader input = new InputStreamReader(s.getInputStream());
        BufferedReader reader = new BufferedReader(input);
        String msg= reader.readLine();
        reader.close();
        ss = (TextView)findViewById(R.id.temp);
        ss.setText(msg);
    }

    catch(Exception ex){
        t = Toast.makeText(getApplicationContext(),ex.toString() , Toast.LENGTH_LONG);
        t.show();
}
}

1 个答案:

答案 0 :(得分:0)

getIpAddress()似乎返回了一个不存在的主机名,或者至少找不到。

错误消息只表示手机无法将String中的address解析为数字IP地址。