0
我想連接一個Android設備到另一個使用WiFi。一臺設備充當使用熱點的服務器。另一個設備連接到它。但是,當我運行以下代碼段時,它會給出以下例外。無法連接到服務器通過WiFi在Android
java.net.ConnectException: failed to connect to /192.168.43.198 (port 5555): connect failed: ENETUNREACH (Network is unreachable)
我正在使用下面的文件。 HostActivity.java
public class HostActivity extends Activity {
ListView lvHost;
HostAdapter adHost;
final String HostTAG = "Host1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host);
WifiManager wifiManager = (WifiManager)this.getSystemService (Context.WIFI_SERVICE);
new RetrieveFeedTask2(getApplicationContext()).execute("");
int i =0 ;
lvHost = (ListView) findViewById(R.id.lvHost);
int j = 0;
ArrayList<WifiConfiguration> list = (ArrayList<WifiConfiguration>) wifiManager.getConfiguredNetworks();
adHost = new HostAdapter(list,this);
lvHost.setAdapter(adHost);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.host, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
class RetrieveFeedTask2 extends AsyncTask<String, Context, String> {
Context ctx = null;
public RetrieveFeedTask2(Context Dctx) {
// TODO Auto-generated constructor stub
ctx = Dctx;
}
protected String doInBackground(String ...url) {
final String HostTAG = "Host1";
final WifiManager wifiManager = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
try {
Boolean end = false;
Log.i("test","HostRun");
ServerSocket ss = new ServerSocket(5555);
while(!end){
Log.i("test", "HostRun2");
//Server is waiting for client here, if needed
Socket s = ss.accept();
Log.i("test", "HostRun3");
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
Log.i("test", "From client: "+st);
System.out.println("From client: "+st);
//output.println("Good bye and thanks for all the fish :)");
s.close();
// ArrayList<WifiConfiguration> list = (ArrayList<WifiConfiguration>) wifiManager.getConfiguredNetworks();
}
ss.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.i("test","host unknown");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String feed) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
ClientActivity.java
public class ClientActivity extends Activity {
final String ClientTAG = "Client1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
WifiManager wifiManager = (WifiManager)getSystemService("wifi");
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = Formatter.formatIpAddress(ipAddress);
new RetrieveFeedTask().execute(ip);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.client, menu);
return true;
}
}
class RetrieveFeedTask extends AsyncTask<String, String, String> {
//Context context;
protected String doInBackground(String ...urls) {
final String ClientTAG = "Client1";
InetAddress ia=null;
Log.i("test", "ClientRun1");
try {
try {
ia=InetAddress.getLocalHost();
Log.i("test",ia.toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Socket s = new Socket("192.168.43.198",5555);
Log.i("test", "ClientRun");
//outgoing stream redirect to socket
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println("Hello Android!");
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
//read line(s)
String st = input.readLine();
//. . .
//Close connection
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String feed) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
我試着用搜索引擎。許多人都提到了同樣的問題,我嘗試了所有的解決方案,但仍然無法連接。請幫忙。
您是否檢查過您的清單以獲取所有權限? – shyam 2014-08-27 10:30:48
我給了所有的權限即。 android.permission.ACCESS_WIFI_STATE,android.permission.CHANGE_WIFI_STATE,android.permission.INTERNET android.permission.ACCESS_NETWORK_STATE – user3482672 2014-08-27 10:37:54
您確定110%「192.168.43.198」是您的主機設備的實際地址嗎?和客戶端設備在同一個網絡上?你有一些Ping應用程序來檢查? – Fildor 2014-08-27 10:58:20