com.socks.jiandan.model.FreshNews Java Examples

The following examples show how to use com.socks.jiandan.model.FreshNews. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FreshNewsParser.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
@Nullable
public ArrayList<FreshNews> parse(Response response) {

    if (!response.isSuccessful())
        return null;

    try {
        String body = response.body().string();
        JSONObject resultObj = new JSONObject(body);
        JSONArray postsArray = resultObj.optJSONArray("posts");
        return FreshNews.parse(postsArray);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #2
Source File: FreshNewsCache.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
@Override
public ArrayList<FreshNews> getCacheByPage(int page) {

    QueryBuilder<com.socks.greendao.FreshNewsCache> query = mFreshNewsCacheDao.queryBuilder().where(FreshNewsCacheDao
            .Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        try {
            return FreshNews.parseCache(new JSONArray(query.list().get(0)
                    .getResult()));
        } catch (JSONException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    } else {
        return new ArrayList<>();
    }

}
 
Example #3
Source File: JDApi.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
public static Observable<ArrayList<FreshNews>> getFreshNews(final int page) {

        return Observable.create(new Observable.OnSubscribe<ArrayList<FreshNews>>() {

            @Override
            public void call(Subscriber<? super ArrayList<FreshNews>> subscriber) {
                String url = FreshNews.getUrlFreshNews(page);
                try {
                    subscriber.onNext(new FreshNewsParser().parse(OkHttpProxy.get()
                            .url(url)
                            .execute()));
                    subscriber.onCompleted();
                } catch (IOException e) {
                    subscriber.onError(e);
                }
            }
        }).compose(JDApi.<ArrayList<FreshNews>>applySchedulers());
    }
 
Example #4
Source File: FreshNewsCache.java    From JianDan_OkHttpWithVolley with Apache License 2.0 6 votes vote down vote up
@Override
public ArrayList<FreshNews> getCacheByPage(int page) {

    QueryBuilder<com.socks.greendao.FreshNewsCache> query = mFreshNewsCacheDao.queryBuilder().where(FreshNewsCacheDao
            .Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        try {
            return FreshNews.parseCache(new JSONArray(query.list().get(0)
                    .getResult()));
        } catch (JSONException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    } else {
        return new ArrayList<>();
    }

}
 
Example #5
Source File: FreshNewsAdapter.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
private void loadFromCache() {
    Observable.create(new Observable.OnSubscribe<ArrayList<FreshNews>>() {
        @Override
        public void call(Subscriber<? super ArrayList<FreshNews>> subscriber) {
            subscriber.onNext(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            subscriber.onCompleted();
        }
    }).compose(JDApi.applySchedulers())
            .doOnNext(freshNewses -> {
                if (page == 1) {
                    mFreshNews.clear();
                    ToastHelper.Short(ConstantString.LOAD_NO_NETWORK);
                }
            })
            .subscribe(freshNewses -> {
                mFreshNews.addAll(freshNewses);
                notifyDataSetChanged();
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                mLoadFinisCallBack.loadFinish(null);
            });
}
 
Example #6
Source File: FreshNewsCache.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
@Override
public ArrayList<FreshNews> getCacheByPage(int page) {

    QueryBuilder<com.socks.greendao.FreshNewsCache> query = mFreshNewsCacheDao.queryBuilder().where(FreshNewsCacheDao
            .Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        try {
            return FreshNews.parseCache(new JSONArray(query.list().get(0)
                    .getResult()));
        } catch (JSONException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    } else {
        return new ArrayList<>();
    }

}
 
Example #7
Source File: FreshNewsParser.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
@Nullable
public ArrayList<FreshNews> parse(Response response) {

    code = wrapperCode(response.code());
    if (!response.isSuccessful())
        return null;

    try {
        String body = response.body().string();
        JSONObject resultObj = new JSONObject(body);
        JSONArray postsArray = resultObj.optJSONArray("posts");
        return FreshNews.parse(postsArray);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #8
Source File: FreshNewsCache.java    From JianDan with Apache License 2.0 6 votes vote down vote up
@Override
public ArrayList<FreshNews> getCacheByPage(int page) {

    QueryBuilder<com.socks.greendao.FreshNewsCache> query = mFreshNewsCacheDao.queryBuilder().where(FreshNewsCacheDao
            .Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        try {
            return FreshNews.parseCache(new JSONArray(query.list().get(0)
                    .getResult()));
        } catch (JSONException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    } else {
        return new ArrayList<>();
    }

}
 
Example #9
Source File: FreshNewsDetailActivity.java    From JianDan with Apache License 2.0 5 votes vote down vote up
@Override
protected void initData() {
    ArrayList<FreshNews> FreshNews = (ArrayList<FreshNews>) getIntent().getSerializableExtra
            (DATA_FRESH_NEWS);
    int position = getIntent().getIntExtra(DATA_POSITION, 0);
    viewPager.setAdapter(new FreshNewsDetailAdapter(getSupportFragmentManager(), FreshNews));
    viewPager.setCurrentItem(position);
}
 
Example #10
Source File: FreshNewsDetailActivity.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
@Override
protected void loadData() {
    Intent intent = getIntent();
    int position = intent.getIntExtra(DATA_POSITION, 0);
    Serializable serializable = intent.getSerializableExtra(DATA_FRESH_NEWS);
    ArrayList<FreshNews> FreshNews = (ArrayList<FreshNews>) serializable;

    viewPager.setAdapter(new FreshNewsDetailAdapter(getSupportFragmentManager(), FreshNews));
    viewPager.setCurrentItem(position);
}
 
Example #11
Source File: FreshNewsDetailFragment.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
public static FreshNewsDetailFragment getInstance(FreshNews freshNews) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(DATA_FRESH_NEWS, freshNews);
    FreshNewsDetailFragment fragment = new FreshNewsDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
}
 
Example #12
Source File: TextUtil.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
public static String getHtml(FreshNews freshNews, String content) {
    final StringBuilder sb = new StringBuilder();
    sb.append("<!DOCTYPE html>");
    sb.append("<html dir=\"ltr\" lang=\"zh\">");
    sb.append("<head>");
    sb.append("<meta name=\"viewport\" content=\"width=100%; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />");
    sb.append("<link rel=\"stylesheet\" href='file:///android_asset/style.css' type=\"text/css\" media=\"screen\" />");
    sb.append("</head>");
    sb.append("<body style=\"padding:0px 8px 8px 8px;\">");
    sb.append("<div id=\"pagewrapper\">");
    sb.append("<div id=\"mainwrapper\" class=\"clearfix\">");
    sb.append("<div id=\"maincontent\">");
    sb.append("<div class=\"post\">");
    sb.append("<div class=\"posthit\">");
    sb.append("<div class=\"postinfo\">");
    sb.append("<h2 class=\"thetitle\">");
    sb.append("<a>");
    sb.append(freshNews.title);
    sb.append("</a>");
    sb.append("</h2>");
    sb.append(freshNews.author.getName() + " @ " + String2TimeUtil
            .dateString2GoodExperienceFormat(freshNews.date));
    sb.append("</div>");
    sb.append("<div class=\"entry\">");
    sb.append(content);
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</body>");
    sb.append("</html>");
    return sb.toString();
}
 
Example #13
Source File: JDApi.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
public static Observable<String> getFreshDetail(String id) {
    return Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            String url = FreshNews.getUrlFreshNewsDetail(id);
            try {
                subscriber.onNext(new FreshNewsDetailParser().parse(OkHttpProxy.get().url(url).execute()));
                subscriber.onCompleted();
            } catch (IOException e) {
                subscriber.onError(e);
            }
        }
    }).compose(applySchedulers());
}
 
Example #14
Source File: Request4FreshNews.java    From JianDan with Apache License 2.0 5 votes vote down vote up
@Override
protected Response<ArrayList<FreshNews>> parseNetworkResponse(NetworkResponse response) {

	try {
		String resultStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		JSONObject resultObj = new JSONObject(resultStr);
		JSONArray postsArray = resultObj.optJSONArray("posts");
		return Response.success(FreshNews.parse(postsArray), HttpHeaderParser.parseCacheHeaders(response));
	} catch (Exception e) {
		e.printStackTrace();
		return Response.error(new ParseError(e));
	}
}
 
Example #15
Source File: FreshNewsDetailFragment.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
public static FreshNewsDetailFragment getInstance(FreshNews freshNews) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(DATA_FRESH_NEWS, freshNews);
    FreshNewsDetailFragment fragment = new FreshNewsDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
}
 
Example #16
Source File: FreshNewsDetailFragment.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
private static String getHtml(FreshNews freshNews, String content) {
    final StringBuilder sb = new StringBuilder();
    sb.append("<!DOCTYPE html>");
    sb.append("<html dir=\"ltr\" lang=\"zh\">");
    sb.append("<head>");
    sb.append("<meta name=\"viewport\" content=\"width=100%; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />");
    sb.append("<link rel=\"stylesheet\" href='file:///android_asset/style.css' type=\"text/css\" media=\"screen\" />");
    sb.append("</head>");
    sb.append("<body style=\"padding:0px 8px 8px 8px;\">");
    sb.append("<div id=\"pagewrapper\">");
    sb.append("<div id=\"mainwrapper\" class=\"clearfix\">");
    sb.append("<div id=\"maincontent\">");
    sb.append("<div class=\"post\">");
    sb.append("<div class=\"posthit\">");
    sb.append("<div class=\"postinfo\">");
    sb.append("<h2 class=\"thetitle\">");
    sb.append("<a>");
    sb.append(freshNews.getTitle());
    sb.append("</a>");
    sb.append("</h2>");
    sb.append(freshNews.getAuthor().getName() + " @ " + String2TimeUtil
            .dateString2GoodExperienceFormat(freshNews.getDate()));
    sb.append("</div>");
    sb.append("<div class=\"entry\">");
    sb.append(content);
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</body>");
    sb.append("</html>");
    return sb.toString();
}
 
Example #17
Source File: FreshNewsDetailFragment.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private static String getHtml(FreshNews freshNews, String content) {
    final StringBuilder sb = new StringBuilder();
    sb.append("<!DOCTYPE html>");
    sb.append("<html dir=\"ltr\" lang=\"zh\">");
    sb.append("<head>");
    sb.append("<meta name=\"viewport\" content=\"width=100%; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />");
    sb.append("<link rel=\"stylesheet\" href='file:///android_asset/style.css' type=\"text/css\" media=\"screen\" />");
    sb.append("</head>");
    sb.append("<body style=\"padding:0px 8px 8px 8px;\">");
    sb.append("<div id=\"pagewrapper\">");
    sb.append("<div id=\"mainwrapper\" class=\"clearfix\">");
    sb.append("<div id=\"maincontent\">");
    sb.append("<div class=\"post\">");
    sb.append("<div class=\"posthit\">");
    sb.append("<div class=\"postinfo\">");
    sb.append("<h2 class=\"thetitle\">");
    sb.append("<a>");
    sb.append(freshNews.getTitle());
    sb.append("</a>");
    sb.append("</h2>");
    sb.append(freshNews.getAuthor().getName() + " @ " + String2TimeUtil
            .dateString2GoodExperienceFormat(freshNews.getDate()));
    sb.append("</div>");
    sb.append("<div class=\"entry\">");
    sb.append(content);
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</body>");
    sb.append("</html>");
    return sb.toString();
}
 
Example #18
Source File: FreshNewsDetailActivity.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
@Override
protected void initData() {
    ArrayList<FreshNews> FreshNews = (ArrayList<FreshNews>) getIntent().getSerializableExtra
            (DATA_FRESH_NEWS);
    int position = getIntent().getIntExtra(DATA_POSITION, 0);
    viewPager.setAdapter(new FreshNewsDetailAdapter(getSupportFragmentManager(), FreshNews));
    viewPager.setCurrentItem(position);
}
 
Example #19
Source File: FreshNewsDetailFragment.java    From JianDan with Apache License 2.0 5 votes vote down vote up
public static FreshNewsDetailFragment getInstance(FreshNews freshNews) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(DATA_FRESH_NEWS, freshNews);
    FreshNewsDetailFragment fragment = new FreshNewsDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
}
 
Example #20
Source File: Request4FreshNews.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
@Override
protected Response<ArrayList<FreshNews>> parseNetworkResponse(NetworkResponse response) {

	try {
		String resultStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		JSONObject resultObj = new JSONObject(resultStr);
		JSONArray postsArray = resultObj.optJSONArray("posts");
		return Response.success(FreshNews.parse(postsArray), HttpHeaderParser.parseCacheHeaders(response));
	} catch (Exception e) {
		e.printStackTrace();
		return Response.error(new ParseError(e));
	}
}
 
Example #21
Source File: Request4FreshNews.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
@Override
protected Response<ArrayList<FreshNews>> parseNetworkResponse(NetworkResponse response) {

	try {
		String resultStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		JSONObject resultObj = new JSONObject(resultStr);
		JSONArray postsArray = resultObj.optJSONArray("posts");
		return Response.success(FreshNews.parse(postsArray), HttpHeaderParser.parseCacheHeaders(response));
	} catch (Exception e) {
		e.printStackTrace();
		return Response.error(new ParseError(e));
	}
}
 
Example #22
Source File: FreshNewsDetailActivity.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
@Override
protected void initData() {
    ArrayList<FreshNews> FreshNews = (ArrayList<FreshNews>) getIntent().getSerializableExtra
            (DATA_FRESH_NEWS);
    int position = getIntent().getIntExtra(DATA_POSITION, 0);
    viewPager.setAdapter(new FreshNewsDetailAdapter(getSupportFragmentManager(), FreshNews));
    viewPager.setCurrentItem(position);
}
 
Example #23
Source File: FreshNewsDetailFragment.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
private static String getHtml(FreshNews freshNews, String content) {
    final StringBuilder sb = new StringBuilder();
    sb.append("<!DOCTYPE html>");
    sb.append("<html dir=\"ltr\" lang=\"zh\">");
    sb.append("<head>");
    sb.append("<meta name=\"viewport\" content=\"width=100%; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />");
    sb.append("<link rel=\"stylesheet\" href='file:///android_asset/style.css' type=\"text/css\" media=\"screen\" />");
    sb.append("</head>");
    sb.append("<body style=\"padding:0px 8px 8px 8px;\">");
    sb.append("<div id=\"pagewrapper\">");
    sb.append("<div id=\"mainwrapper\" class=\"clearfix\">");
    sb.append("<div id=\"maincontent\">");
    sb.append("<div class=\"post\">");
    sb.append("<div class=\"posthit\">");
    sb.append("<div class=\"postinfo\">");
    sb.append("<h2 class=\"thetitle\">");
    sb.append("<a>");
    sb.append(freshNews.getTitle());
    sb.append("</a>");
    sb.append("</h2>");
    sb.append(freshNews.getAuthor().getName() + " @ " + String2TimeUtil
            .dateString2GoodExperienceFormat(freshNews.getDate()));
    sb.append("</div>");
    sb.append("<div class=\"entry\">");
    sb.append(content);
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</div>");
    sb.append("</body>");
    sb.append("</html>");
    return sb.toString();
}
 
Example #24
Source File: FreshNewsDetailFragment.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
public static FreshNewsDetailFragment getInstance(FreshNews freshNews) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(DATA_FRESH_NEWS, freshNews);
    FreshNewsDetailFragment fragment = new FreshNewsDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
}
 
Example #25
Source File: Request4FreshNews.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<FreshNews> response) {
	listener.onResponse(response);
}
 
Example #26
Source File: FreshNewsDetailActivity.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
public FreshNewsDetailAdapter(FragmentManager fm, ArrayList<FreshNews> freshNewses) {
    super(fm);
    this.freshNewses = freshNewses;
}
 
Example #27
Source File: Request4FreshNews.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
public Request4FreshNews(String url, Response.Listener<ArrayList<FreshNews>> listener,
                         Response.ErrorListener errorListener) {
	super(Method.GET, url, errorListener);
	this.listener = listener;
}
 
Example #28
Source File: FreshNewsAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            RequestManager.addRequest(new Request4FreshNews(FreshNews.getUrlFreshNews(page),
                    new Response.Listener<ArrayList<FreshNews>>() {
                        @Override
                        public void onResponse(ArrayList<FreshNews> response) {

                            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                            mLoadFinisCallBack.loadFinish(null);

                            if (page == 1) {
                                mFreshNews.clear();
                                FreshNewsCache.getInstance(mActivity).clearAllCache();
                            }

                            mFreshNews.addAll(response);
                            notifyDataSetChanged();

                            FreshNewsCache.getInstance(mActivity).addResultCache(JSONParser.toString(response),
                                    page);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                    mLoadFinisCallBack.loadFinish(null);
                }
            }), mActivity);
        } else {
            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            mLoadFinisCallBack.loadFinish(null);

            if (page == 1) {
                mFreshNews.clear();
                ShowToast.Short(ConstantString.LOAD_NO_NETWORK);
            }

            mFreshNews.addAll(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            notifyDataSetChanged();
        }

    }
 
Example #29
Source File: FreshNewsDetailAdapter.java    From JianDanRxJava with Apache License 2.0 4 votes vote down vote up
public FreshNewsDetailAdapter(FragmentManager fm, ArrayList<FreshNews> freshNewses) {
    super(fm);
    this.freshNewses = freshNewses;
}
 
Example #30
Source File: IntentHelper.java    From JianDanRxJava with Apache License 2.0 4 votes vote down vote up
public static void toFreshDetailActivity(Context context, ArrayList<FreshNews> mFreshNews, int position) {
    Intent intent = new Intent(context, FreshNewsDetailActivity.class);
    intent.putExtra(FreshNewsDetailActivity.DATA_FRESH_NEWS, mFreshNews);
    intent.putExtra(FreshNewsDetailActivity.DATA_POSITION, position);
    context.startActivity(intent);
}