我正在开发 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();
}
}