Java Code Examples for com.socks.jiandan.model.FreshNews

The following examples show how to use com.socks.jiandan.model.FreshNews. These examples are extracted from open source projects. 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 Project: JianDan_OkHttpWithVolley   Source File: FreshNewsCache.java    License: 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 2
Source Project: JianDan   Source File: FreshNewsCache.java    License: 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 Project: JianDanRxJava   Source File: FreshNewsAdapter.java    License: 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 4
Source Project: JianDanRxJava   Source File: FreshNewsCache.java    License: 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 Project: JianDanRxJava   Source File: FreshNewsParser.java    License: 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 6
Source Project: JianDanRxJava   Source File: JDApi.java    License: 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 7
Source Project: JianDan_OkHttp   Source File: FreshNewsParser.java    License: 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 Project: JianDan_OkHttp   Source File: FreshNewsCache.java    License: 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
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 10
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 11
@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 12
@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 13
Source Project: JianDan   Source File: FreshNewsDetailFragment.java    License: 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 14
Source Project: JianDan   Source File: FreshNewsDetailFragment.java    License: 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 15
Source Project: JianDan   Source File: FreshNewsDetailActivity.java    License: 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 16
Source Project: JianDan   Source File: Request4FreshNews.java    License: 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 17
Source Project: JianDanRxJava   Source File: TextUtil.java    License: 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 18
Source Project: JianDanRxJava   Source File: FreshNewsDetailFragment.java    License: 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 19
Source Project: JianDanRxJava   Source File: FreshNewsDetailActivity.java    License: 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 20
Source Project: JianDanRxJava   Source File: JDApi.java    License: 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 21
Source Project: JianDan_OkHttp   Source File: FreshNewsDetailFragment.java    License: 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 22
Source Project: JianDan_OkHttp   Source File: FreshNewsDetailFragment.java    License: 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 23
Source Project: JianDan_OkHttp   Source File: FreshNewsDetailActivity.java    License: 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 24
Source Project: JianDan_OkHttp   Source File: Request4FreshNews.java    License: 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 25
Source Project: JianDan_OkHttpWithVolley   Source File: FreshNewsAdapter.java    License: 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 26
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    setHasOptionsMenu(true);
    freshNews = (FreshNews) getArguments().getSerializable(DATA_FRESH_NEWS);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            if (newProgress > 80) {
                loading.stop();
            }
        }
    });
    executeRequest(new Request4FreshNewsDetail(FreshNews.getUrlFreshNewsDetail(freshNews.getId()),
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    webView.loadDataWithBaseURL("", getHtml(freshNews, response), "text/html", "utf-8", "");
                }
            }, new
            Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }));
    loading.start();

    loading.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (loading.isShown()) {
                loading.stop();
            }
        }
    }, 10 * 1000);

}
 
Example 27
public FreshNewsDetailAdapter(FragmentManager fm, ArrayList<FreshNews> freshNewses) {
    super(fm);
    this.freshNewses = freshNewses;
}
 
Example 28
public Request4FreshNews(String url, Response.Listener<ArrayList<FreshNews>> listener,
                         Response.ErrorListener errorListener) {
	super(Method.GET, url, errorListener);
	this.listener = listener;
}
 
Example 29
@Override
protected void deliverResponse(ArrayList<FreshNews> response) {
	listener.onResponse(response);
}
 
Example 30
Source Project: JianDan   Source File: FreshNewsAdapter.java    License: 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();
        }

    }