Android:无法从App连接到互联网?

时间:2015-09-07 06:29:40

标签: java android email android-studio

我正在尝试从android studio中的this运行代码,并进行一些修改以将信息转发到我的电子邮件帐户。

但我无法ping或从我的应用程序连接到gmail smtp。我添加了以下代码来检查互联网连接,但ping google.com返回false并且checknetworkstatus同时返回true。我连接到我的Wifi网络。

c.getName()

我正在使用link中的代码发送电子邮件,并将以下权限添加到我的清单文件中,但仍然无效。

        public static boolean ping(String url, int timeout) {
    // Otherwise an exception may be thrown on invalid SSL certificates:
    url = url.replaceFirst("^https", "http");

    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);
        try {
            connection.setRequestMethod("HEAD");

            int responseCode = connection.getResponseCode();
            System.err.println("URL " + url + " returned: " + responseCode);
            return ((200 <= responseCode && responseCode <= 399) || (responseCode == 405));
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
    } catch (IOException exception) {
        return false;
    }
    return false;
}

public boolean checkNetworkStatus() {

    // Variables used for checking the network state.
    final ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    final NetworkInfo wifi = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    final NetworkInfo mobile = connMgr
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if ((wifi.isConnected() == true) || (mobile.isConnected() == true)) {
        return true;
    } else
        return false;
}

并生成错误

     <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />

这是android studio或其他东西的某种错误吗?

0 个答案:

没有答案