2012-04-23 71 views
0

如何從連接類文件調用connectivityCheck方法? [Android]產品如何從連接類文件調用connectivityCheck方法? [Android]

例如,代碼@連通類文件

public class ConnectivityMgr { 
/* 
*@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; 
    } 
} 

我有什麼做的,從類調用此方法,例如,如果它是一個final.last叫包下。測試

我一定要導入的類文件,並聲明或類似的東西,我嘗試使用

if (isNetworkAvailable(getApplicationContext()) == true) {} 

,它給了我一個NullPointerException。是「getApplicationContext()」還是其他的錯誤用法?我嘗試導入,聲明..,改變this.getApplicationContext()/ this/getBaseContext(),它仍然不起作用。

還是需要一個「意圖」?

我做了什麼:

import final.last.test.ConnectivityMgr; 
ConnectivityMgr cm; 

try { 

if (cm.isNetworkAvailable(getApplicationContext()) 
{ //display yes } 

} Catch as (Exception e) 
{ Toast.makeText(getApplicationContext(), "Error with connectivity : " + e.getCause() + " , " + e.toString() , Toast.LENGTH_LONG).show(); 

// Log.d("Error With Connectivity : ","Failed to do something due to : " + e.getMessage()); 

- >日誌之前從未露面,這就是爲什麼我用麪包來代替。

- >吐司顯示「錯誤與連通性:空,java.lang中,NullPointerException異常 -

+0

顯示調用代碼 – 2012-04-23 04:48:02

+0

ConnectivityMgr.isNetworkAvailable(getApplicationContext( ))和imprt「ConnectivityMgr包」.ConnectivityMgr.Show logcat輸出,如果你仍然有錯誤。 – 2012-04-23 04:56:55

+0

我所做的是... import final.last.test.ConnectivityMgr; ConnectivityMgr cm; 嘗試 { 如果(cm.isNetworkAvailable(getApplicationContext()){ // 顯示是 } } 捕捉儘可能(例外五) \t \t { \t \t \t \t \t \t \t \t \t吐司。「連接錯誤:」+ e.getCause()+「,」+ e.toString(),Toast.LENGTH_LONG).show(); \t \t \t // Log.d(「連接錯誤」,「無法執行某些操作,因爲:」+ e.getMessage()); \t - >日誌從來沒有爲我工作。 (if)(cf.isNetworkAvailable(getApplicationContext());} Toast shows「null,java.lang,NullPointerException – JimmyPW93 2012-04-23 06:59:41

回答

0
public static boolean isNetworkAvailable(Context context) { 
     boolean mBoolean = false; 
     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) { 
         mBoolean = true; 
        } 
       } 
      } 
     } 
     return mBoolean; 
    } 

然後

if (isNetworkAvailable(getApplicationContext())) {} 
+0

// Question-er edit // import(package)。(class file); // classfile cf; // if ))){} //然而,我用try catch捕獲nullpointexception。 – JimmyPW93 2012-04-23 07:35:56

0
 URL myurl = new URL(url); 
     URLConnection connection = myurl.openConnection(); 
     connection.setConnectTimeout(20 * 1000); 
     HttpURLConnection httpConnection = (HttpURLConnection) connection; 
     int responseCode = httpConnection.getResponseCode(); 
     if (responseCode == HttpURLConnection.HTTP_OK) 
     { 
      httpConnection.disconnect(); 
      //...... 

     } 
     else 
     { 
      httpConnection.disconnect(); 
      throw new Exception(
        "Http Error code: " + String.valueOf(responseCode)); 
     }