Android Open Source - cnBeta Article List Service






From Project

Back to project page cnBeta.

License

The source code is released under:

Apache License

If you think the Android project cnBeta listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

?package com.guest.cnbeta.service;
/*w  w w.  j  a  v  a2  s.  c om*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import android.content.Context;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.guest.cnbeta.database.ArticleDB;
import com.guest.cnbeta.loadsource.LoadSource;
import com.guest.cnbeta.loadsource.LoadSourceInterface;
import com.guest.cnbeta.module.Article;

public class ArticleListService extends BaseService {
  private LoadSourceInterface loadSource = new LoadSource();
  private ArticleDB articleDB;

  public ArticleListService(Context context) {
    super(context);
    articleDB = new ArticleDB(context);
  }

  public List<Article> getArticleListFromWeb() throws MalformedURLException,
      IOException {
    return loadSource.getArticleListFromWeb();
  }

  public List<Article> getArticleList(int page) {

    List<Article> list = null;

    try {
      list = loadSource.getMoreArticleListFromWeb(page);
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NullPointerException e) {
      // TODO: handle exception
      e.printStackTrace();
    }

    return list;
  }

  public void syncArticleToDb(List<Article> list) {
    for (Article article : list) {
      articleDB.save(article);
    }
  }
  
  public List<Article> getArticleListFromDB() {
    return articleDB.getList();
  }

  public void offLineDownload(Context context, List<Article> articleList) {

    ArticleService cbArticleService = new ArticleService(context);

    try {
      for (Article article : articleList) {
        if (!article.isContented()) {
          Article mArticle = cbArticleService.getMoreDetail(article);
          FileOutputStream out = context.openFileOutput("cache_art_"
              + mArticle.getId() + ".html", Context.MODE_PRIVATE);
          if (mArticle.getContent() != null) {
            out.write(mArticle.getContent().getBytes());
          } else {
            out.write("<html>404</html>".getBytes());
          }
          out.flush();
          out.close();

          WebView webView = new WebView(context);
          LayoutParams layoutParams = new LayoutParams(
              LayoutParams.WRAP_CONTENT,
              LayoutParams.WRAP_CONTENT);
          webView.setLayoutParams(layoutParams);
          webView.getSettings()
              .setCacheMode(WebSettings.LOAD_NORMAL);
          webView.getSettings().setLoadsImagesAutomatically(true);
          webView.loadUrl("file://" + context.getFilesDir()
              + "/cache_art_" + article.getId() + ".html");
        }
      }

      syncArticleToDb(articleList);
      
    } catch (MalformedURLException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (NullPointerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}




Java Source Code List

.ArticleActivity.java
.ArticleListService.java
.ArticleService.java
.Article.java
.CommentActivity.java
.CommentService.java
.Comment.java
.LoadSourceInterface.java
.LoadSource.java
.MainActivity.java
.PostCommentActivity.java
.Util.java
com.guest.cnbeta.FirstActivity.java
com.guest.cnbeta.SettingsActivity.java
com.guest.cnbeta.database.ArticleDB.java
com.guest.cnbeta.database.BaseDBHelper.java
com.guest.cnbeta.database.BaseDB.java
com.guest.cnbeta.loadsource.BaseLoadSource.java
com.guest.cnbeta.module.Avatar.java
com.guest.cnbeta.service.AvatarListService.java
com.guest.cnbeta.service.BaseService.java
com.guest.cnbeta.util.BaseApplication.java
com.guest.cnbeta.util.DataEngine.java
com.guest.cnbeta.util.FlingGallery.java
com.guest.cnbeta.util.NetworkStatusReceiver.java
com.guest.cnbeta.util.PullToRefreshListView.java
com.guest.cnbeta.util.ScaleAnimationHelper.java
com.guest.cnbeta.util.T.java