2011-12-30 50 views
0

在我的应用我解析从网上进像下面的代码:如何处理网络可用性这段代码

private void downloadEpisodes(String Url) { 
    //Make Progress Bar Visible While Downloading Feed 
    progress_bar.setVisibility(ProgressBar.VISIBLE); 
    Log.d("CGRParser", "Downloading Feed"); 
    //Start an ASync Thread to take care of Downloading Feed 
    new DownloadEpisodes().execute(Url); 
} 

private class DownloadEpisodes extends AsyncTask<String, Integer, ArrayList<LatestNews>> { 
    ArrayList<LatestNews> episodes; 
    @Override 
    protected ArrayList<LatestNews> doInBackground(String... url) { 

     //Download and Parse Feed 
     XmlFeedParser parser = new XmlFeedParser(); 
     episodes = new ArrayList<LatestNews>(); 
     episodes = parser.parse(url[0]); 
     return episodes; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<LatestNews> result) { 
     //Feed has been Downloaded and Parsed, Display Data to User 
     Log.d("CGRParser", "Feed Download Complete"); 
     displayEpisodes(result); 
    } 
} 

private void displayEpisodes(ArrayList<LatestNews> news_detail) { 
    //Create String Arrays to seperate titles and dates 
    Log.d("CGRParser", "Displaying News Titles To User"); 
    ArrayList<String> episode_titles = new ArrayList<String>(); 
    ArrayList<String> episode_description = new ArrayList<String>(); 
    ArrayList<String> episode_link = new ArrayList<String>(); 
    for (LatestNews news : news_detail) { 
     Log.d("News", "News Title: " + news.getTitle()); 
     Log.d("News", "News Description: " + news.getDescription()); 
     Log.d("News", "News Link: " + news.getLink()); 
     episode_titles.add(news.getTitle()); 
     episode_description.add(news.getDescription()); 
     episode_link.add(news.getLink()); 
    } 

    this.episode_titles = episode_titles; 
    this.episode_description = episode_description; 
    this.episode_link = episode_link; 

    //Create a ListAdapter to Display the Titles in the ListView 
    ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.latest_news_row, R.id.title, episode_titles); 
    listview_news.setAdapter(adapter); 
    //Set Progress Bar Invisible since we are done with it 
    progress_bar.setVisibility(ProgressBar.INVISIBLE); 

} 

现在有了这个代码,我要处理的网络可用性。如果internate不可用,那么消息应显示为“请检查internate连接”。

请帮我。 谢谢。

+0

有很多的例子已经可以在SO本身。 – 2011-12-30 10:35:14

回答

2

您必须将网络状态权限放入清单中。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

然后你就可以检查连接可用,像这样一个函数(有人贴吧eralier在计算器和我用它在我的应用程序):

public boolean isOnline() { 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } 
    return false; 
} 

然后你只需检查如果isOnline在您的活动中,并显示Toast或其他对话框(如果不是)。我认为你应该在task.execute()之前在你的活动中进行检查,而不是在AsyncTask中。

+0

感谢它很好。 – 2011-12-30 10:39:17

1

如何检查网络可用性

public static boolean isNetworkPresent(Context context) { 
     boolean isNetworkAvailable = false; 
     ConnectivityManager cm = (ConnectivityManager) context 
       .getSystemService(Context.CONNECTIVITY_SERVICE); 
     try { 

      if (cm != null) { 
       NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
       if (netInfo != null) { 
        isNetworkAvailable = netInfo.isConnectedOrConnecting(); 
       } 
      } 
     } catch (Exception ex) { 
      Log.e("Network Avail Error", ex.getMessage()); 
     } 
     //check for wifi also 
     if(!isNetworkAvailable){ 
      WifiManager connec = (WifiManager) context 
        .getSystemService(Context.WIFI_SERVICE); 
      State wifi = cm.getNetworkInfo(1).getState(); 
      if (connec.isWifiEnabled() 
        && wifi.toString().equalsIgnoreCase("CONNECTED")) { 
       isNetworkAvailable = true; 
      } else { 

       isNetworkAvailable = false; 
      } 

     } 
     return isNetworkAvailable; 
    }