简体   繁体   中英

My android application not able to access internet on emulator but works fine on real device

The problem what im facing now is that my app wont connect to internet, i usually run my app on Bluestacks app player and one AVD device and i have give the following permissions

<uses-permission android:name="android.permission.INTERNET" />

Points to be noted:

1)I can access internet through emulator browser.
2)My app canaccess internet if i run it on real device.

Code Sample:

Runtime runtime = Runtime.getRuntime();
        try {
            Process mIpAddrProcess = runtime.exec("/system/bin/ping -w 2 8.8.8.8");
            mExitValue = mIpAddrProcess.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(mIpAddrProcess.getInputStream()));
            int i;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((i = reader.read(buffer)) > 0)
                output.append(buffer, 0, i);
            reader.close();
            String str = output.toString();
            if (mExitValue == 0)
            {
                Toast.makeText(getApplicationContext(),"Connection ok"+ str, Toast.LENGTH_SHORT).show();

            } else
            {

                Toast.makeText(getApplicationContext(), "Please Check Your Internet Connection"+str, Toast.LENGTH_SHORT).show();
                //new Description().execute();
            }
        }

Help me please , Thank you

As you said it works fine with real device but not emulator.

That means code wise your app is correct, moreover there no extra coding we have to do to run an app on emulator.

There must some conflicts while connecting to internet.

If You are using wifi that might be the problem, as emulator tries to contact LAN adapter to get DNS settings.

What to do:

Disable your LAN and then conenct with wifi.

This worked for me. Hope you are having same problem.

This issues is related to the configuration of your emulator to access internet.

And please dont make a ping to check connectivity , instead you can use this method

public static boolean isOnline(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM