我尝试使用wifi连接测试我的项目: 我使用genymotion模拟器4.2.2 模拟器检测到wifi / 3g连接但在我的项目中我无法连接到localhost
it is necessary to change the code or do any configuration on the emulator?
代码:
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
HomeFragment中的:
ConnectionDetector cd;
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(RegisterActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
答案 0 :(得分:1)
Genymotion的模拟器使用Oracle VM VirtualBox,VirtualBox安装驱动程序,用于计算机互联网连接和模拟器之间的直接连接。您是否尝试从模拟器内的android webbrowser访问网页?如果您可以访问该网页,那么您的应用程序内部可能存在问题但是,如果您不能,我会建议重新安装VM虚拟机或直接请求genymotion支持