인터넷에 액세스 할 수 있는지 확인하기 위해이 클래스가 있습니다.
public class NetworkAvailability extends AsyncTask<Context, Boolean, Boolean> {
Context mContext;
private static boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) ListViewActivity.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) {
Log.i("Netzwerkstatus: ", "Netzwerk-Verbindung verfügbar");
return true;
}
return false;
}
public NetworkAvailability(Context mContext) {
this.mContext = mContext;
}
public NetworkAvailability() {
}
@Override
protected Boolean doInBackground(Context... params) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.de").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
try {
urlc.connect();
} catch (IOException e) {
e.printStackTrace();
}
//i ist 200 bei Internetverbindung
int i = urlc.getResponseCode();
return (i == 200);
} catch (IOException e) {
Log.e("IsInternetAccessible: ", "Konnte Internetverbindung nicht prüfen");
}
} else {
Log.d("IsInternetAccessible:", "Internet nicht verfügbar");
}
return false;
}
이제 xml을 구문 분석하기 전에 인터넷에 액세스 할 수 있는지 확인하고 싶습니다. 다음은이 클래스를 사용하려는 스 니펫입니다.
if (networkAvailability.execute()) {
XMLParser parser = new XMLParser();
String xml = parser.getXMLFromUrl("http://XXXXXX.de/XX.xml");
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_SYSTEM);
for (int i = 0; i < nl.getLength(); i++) {
// Neue HashMap erstellen
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// Jedes Kind-Knoten zur HashMap
map.put(KEY_UUID, parser.getValue(e, KEY_UUID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_JOBTITLE, parser.getValue(e, KEY_JOBTITLE));
map.put(KEY_JOBINFO, parser.getValue(e, KEY_JOBINFO));
map.put(KEY_IMAGE, parser.getValue(e, KEY_IMAGE));
//Hashmap zur ArrayList hinzufügen
menuItems.add(map);
}
} else {
Log.i("doInBackground:", "Keine Internet-Verbindung");
return null;
}
내 if 루프에서 NetworkAvailability 클래스의 결과를 조건으로 어떻게 사용할 수 있습니까?
당신이 할 수있는 일은 인터넷 연결을 확인하기 위해 별도의 클래스를 만든 다음 해당 클래스의 객체를 만들고 함수를 호출하여 코드를 정리하는 것입니다.
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
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;
}
}
그런 다음 활동 전역에서 해당 ConnectionDetector에 대한 개체를 만듭니다.
ConnectionDetector cd=new ConnectionDetector(this);
그 함수를 사용하고 싶은 곳이 있으면
if(cd.isConnectingToInternet()){
//internet available
}else{
//internet not available
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다