0

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

安卓客户端

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();
}
}
4

1 回答 1

0

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

错误消息仅表示电话无法将输入解析Stringaddress数字 IP 地址。

于 2013-08-16T18:43:24.787 回答