我的Android应用程序无法上网

时间:2014-10-18 07:55:14

标签: java android

我只是拥有自己的Android应用程序,但我无法连接到它上面

这是我的AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.onscreenapp.app" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <activity
        android:name="com.example.onscreenapp.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

所以你可以看到,我对它有权限。

我尝试使用2个功能来检查互联网状态

一个就是这个:

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name

        if (ipAddr.equals("")) {
            return false;
        } else {
            return true;
        }

    } catch (Exception e) {
        return false;
    }

}

返回false,另一个是这个:

public final boolean isInternetOn() {

    // get Connectivity Manager object to check connection
    ConnectivityManager connec =
            (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);

    // Check for network connections
    if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
            connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {

        // if connected with internet

        Toast.makeText(this, " Connected ", Toast.LENGTH_LONG).show();
        return true;

    } else if (
            connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                    connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {

        Toast.makeText(this, " Not Connected ", Toast.LENGTH_LONG).show();
        return false;
    }
    return false;
}

我正在通过Android S4 mini上的设备测试这个应用程序,当我编译它并启动它时,我得到“appname已停止”显示为运行时错误。

5 个答案:

答案 0 :(得分:1)

将清单下面的行放在应用程序

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

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stackoverflowandroid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.onscreenapp.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>


----------
    public static boolean isInternetAvailable(Context c) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) c
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

使用上方清单和连接可用代码

答案 2 :(得分:0)

isInternetAvailable()方法返回false。因此,这意味着虽然您已连接到网络,但由于以下代码行,您无法访问Internet:

InetAddress ipAddr = InetAddress.getByName("google.com");

基本上是对谷歌的ping。

请发布logcat以获得更好的帮助。

P.S。 - 您也可以在这里参考@Squonk的答案:Detect whether there is an Internet connection available on Android

答案 3 :(得分:0)

很抱歉迟到的回复,但是看起来这个因为我在我的应用程序上运行它在4.4安卓平板电脑和我的日志猫上有同样的问题,它显示了查询互联网连接方法的代码有问题。所以我删除了以下几行,我用双斜线评论了它们,现在我的应用程序正常工作。也许这个案例你必须做同样的事情。希望它对你有用,就像它对我一样。以下是代码......

    //A method that checks to see if the Users phone is connected to the internet or not
    public boolean isConnetedToInternetOrNot() {

          // get Connectivity Manager object to check connection

        connec =    (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);

        if (
             //connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED //||
            // connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING //||
           //  connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
             connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED 

             ) {

             return true;

        } else {

            return false;

        }
    }

答案 4 :(得分:0)

将此代码放入AndroidManifest xml文件中:

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