0
我正在開發Java Server和Android客戶端的簡單程序,當嘗試將我的Android客戶端連接到java服務器時,它總是給出UnknownHostException,我沒有使用仿真器測試實際設備上的整個程序。無法創建套接字。 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();
}
}
非常感謝。我使用「:」而不是「。」。在我的getIpAddress()...但現在我得到「java.net.connectException:連接拒絕」...!我在本地主機上執行相同的代碼及其工作。 – Satyajeet