Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查互联网是否可用,而不是android是否开启了移动或wifi_Android - Fatal编程技术网

如何检查互联网是否可用,而不是android是否开启了移动或wifi

如何检查互联网是否可用,而不是android是否开启了移动或wifi,android,Android,我有两项活动 第一个名为:MainActivity 第二个名字叫:MountLebanonActivity 在第二个活动中,我将此代码用于从网站检索文本 private TextView txtdata; final String textSource = "http://orthodoxprayers.yolasite.com/resources/saint_elie_sinelfil.txt"; @Override protected void onCreate(Bundle savedIn

我有两项活动

第一个名为:MainActivity

第二个名字叫:MountLebanonActivity

在第二个活动中,我将此代码用于从网站检索文本

private TextView txtdata;
final String textSource = "http://orthodoxprayers.yolasite.com/resources/saint_elie_sinelfil.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    setContentView(R.layout.activity_mount_lebanon);
    txtdata = (TextView)findViewById(R.id.txtdata);
    new MyTask().execute();
    URL textUrl;

       try {
        textUrl = new URL(textSource);

        BufferedReader bufferReader 
         = new BufferedReader(new InputStreamReader(textUrl.openStream()));

        String StringBuffer;
        String stringText = "";
        while ((StringBuffer = bufferReader.readLine()) != null) {
         stringText += StringBuffer;   
        }
        bufferReader.close();

        txtdata.setText(stringText);
       } catch (MalformedURLException e) {
        e.printStackTrace();
        txtdata.setText(e.toString());   
       } catch (IOException e) {
        e.printStackTrace();
        txtdata.setText(e.toString());   
       }

   }

   private class MyTask extends AsyncTask<Void, Void, Void>{

    String textResult;

    @Override
    protected Void doInBackground(Void... params) {

        URL textUrl;

        try {
         textUrl = new URL(textSource);

         BufferedReader bufferReader 
          = new BufferedReader(new InputStreamReader(textUrl.openStream()));

         String StringBuffer;
         String stringText = "";
         while ((StringBuffer = bufferReader.readLine()) != null) {
          stringText += StringBuffer;   
         }
         bufferReader.close();

         textResult = stringText;
        } catch (MalformedURLException e) {
         e.printStackTrace();
         textResult = e.toString();   
        } catch (IOException e) {
         e.printStackTrace();
         textResult = e.toString();   
        }

     return null;

    }

    @Override
    protected void onPostExecute(Void result) {

     txtdata.setText(Html.fromHtml(textResult));
     super.onPostExecute(result);   
    }

   }    
如果打开或关闭wifi或手机,此代码工作正常

但如果我的wifi已打开且没有internet连接,则此代码不起作用,因为我没有收到祝酒词,但打开第二个活动时出现错误代码java.net.connectesception:连接失败。。。端口80连接失败(连接被拒绝)


有什么帮助吗?

你说得对。您提供的代码仅检查是否存在网络连接。检查是否存在活动的Internet连接的最佳方法是尝试通过http连接到已知的服务器

public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
    try {
        HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error checking internet connection", e);
    }
} else {
    Log.d(LOG_TAG, "No network available!");
}
return false;
}

你所说的“互联网可用”是什么意思?您的意思是您可以访问曾经连接过的每个主机吗?或者您是否可以连接到当前连接的每台主机?更可能的情况是,您的意思是您可以访问一小部分主机。Ping他们。我的意思是我的wifi已打开,但没有internet连接此代码检查wifi或手机是否已打开,但我需要的是检查是否有internet连接此代码您将永远无法访问其他:(wifi.isConnected()| datac.isConnected()
如果
将作为wifi.isConnected()启动)正在工作。只需检查您是否可以连接到您的url,并且它没有返回连接错误。请在启动活动之前尝试连接。我应该将此代码放在您的广播接收器类中的何处请像这样尝试。
public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
    try {
        HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
        urlc.setRequestProperty("User-Agent", "Test");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1500); 
        urlc.connect();
        return (urlc.getResponseCode() == 200);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error checking internet connection", e);
    }
} else {
    Log.d(LOG_TAG, "No network available!");
}
return false;
}