android连接

android连接,android,networking,Android,Networking,嗨,我正在使用来自 但是,当我从桌面中断internet连接时,它不会发现没有连接,并尝试连接。我正在Android模拟器中运行它。我的问题:为什么它不会返回false并仍然判断它已连接?如果它是通过3D连接的,它不应该获取数据吗?在您的计算机上禁用internet连接不会反映模拟器上的更改 您需要像在真实手机上一样,直接从模拟器禁用internet: 设置->无线和网络->移动网络->使用打包数据。 (我想路径可能因模拟器而异,具体取决于SDK版本)公共类网络{ /* *@返回布尔值如果应用程

嗨,我正在使用来自


但是,当我从桌面中断internet连接时,它不会发现没有连接,并尝试连接。我正在Android模拟器中运行它。我的问题:为什么它不会返回false并仍然判断它已连接?如果它是通过3D连接的,它不应该获取数据吗?

在您的计算机上禁用internet连接不会反映模拟器上的更改

您需要像在真实手机上一样,直接从模拟器禁用internet:

设置->无线和网络->移动网络->使用打包数据。

(我想路径可能因模拟器而异,具体取决于SDK版本)

公共类网络{
/*
*@返回布尔值如果应用程序可以访问internet,则返回true
*/
公共静态布尔值isNetworkAvailable(上下文){
ConnectivityManager connectivity=(ConnectivityManager)context.getSystemService(context.connectivity\u SERVICE);
if(连接性!=null){
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null){
对于(int i=0;i
公共类HttpManager{

public static String getData(String uri) {

    BufferedReader reader = null;

    try {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        return sb.toString();

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}

你不使用xmpp连接吗…好的,我实际上只是在android中迈出了第一步..你在说什么?
        ConnectivityManager connMgr =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        // Checks the user prefs and the network connection. Based on the result, decides
        // whether
        // to refresh the display or keep the current display.
        // If the userpref is Wi-Fi only, checks to see if the device has a Wi-Fi connection.
        if (WIFI.equals(sPref) && networkInfo != null
                && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            // If device has its Wi-Fi connection, sets refreshDisplay
            // to true. This causes the display to be refreshed when the user
            // returns to the app.
            refreshDisplay = true;
            Toast.makeText(context, R.string.wifi_connected, Toast.LENGTH_SHORT).show();

            // If the setting is ANY network and there is a network connection
            // (which by process of elimination would be mobile), sets refreshDisplay to true.
        } else if (ANY.equals(sPref) && networkInfo != null) {
            refreshDisplay = true;

            // Otherwise, the app can't download content--either because there is no network
            // connection (mobile or Wi-Fi), or because the pref setting is WIFI, and there
            // is no Wi-Fi connection.
            // Sets refreshDisplay to false.
        } else {
            refreshDisplay = false;
            Toast.makeText(context, R.string.lost_connection, Toast.LENGTH_SHORT).show();
        }
public class Networking {
 /*
 *@return boolean return true if the application can access the internet
 */
 public static boolean isNetworkAvailable(Context context) {
     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;
  }
}
public static String getData(String uri) {

    BufferedReader reader = null;

    try {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        return sb.toString();

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}