com.socks.jiandan.model.Joke Java Examples

The following examples show how to use com.socks.jiandan.model.Joke. 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: JokeParser.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
@Nullable
public ArrayList<Joke> parse(Response response) {

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

    try {
        String jsonStr = response.body().string();
        jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString();

        Type type = new TypeToken<ArrayList<Joke>>() {
        }.getType();

        return new GsonHelper<ArrayList<Joke>>().fromJson(jsonStr, type);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #2
Source File: Request4Joke.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
@Override
protected Response<ArrayList<Joke>> parseNetworkResponse(NetworkResponse response) {

	try {
		String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString();

		return Response.success((ArrayList<Joke>) JSONParser.toObject(jsonStr,
				new TypeToken<ArrayList<Joke>>() {
				}.getType()), HttpHeaderParser.parseCacheHeaders(response));

	} catch (Exception e) {
		e.printStackTrace();
		return Response.error(new ParseError(e));
	}
}
 
Example #3
Source File: Request4Joke.java    From JianDan_OkHttpWithVolley with Apache License 2.0 6 votes vote down vote up
@Override
protected Response<ArrayList<Joke>> parseNetworkResponse(NetworkResponse response) {

	try {
		String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString();

		return Response.success((ArrayList<Joke>) JSONParser.toObject(jsonStr,
				new TypeToken<ArrayList<Joke>>() {
				}.getType()), HttpHeaderParser.parseCacheHeaders(response));

	} catch (Exception e) {
		e.printStackTrace();
		return Response.error(new ParseError(e));
	}
}
 
Example #4
Source File: JokeAdapter.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
private void loadCache() {
    Subscription subscription = Observable
            .create((Observable.OnSubscribe<ArrayList<Joke>>)
                    subscriber -> {
                        subscriber.onNext(JokeCache.getInstance(mActivity).getCacheByPage(page));
                        subscriber.onCompleted();
                    })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnNext(jokes -> {
                if (page == 1) {
                    mJokes.clear();
                    ToastHelper.Short(ConstantString.LOAD_NO_NETWORK);
                }
            })
            .subscribe(jokes -> {
                mJokes.addAll(jokes);
                notifyDataSetChanged();
                mLoadFinisCallBack.loadFinish(null);
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            });
    mActivity.addSubscription(subscription);
}
 
Example #5
Source File: JokeParser.java    From JianDan_OkHttp with Apache License 2.0 6 votes vote down vote up
@Nullable
public ArrayList<Joke> parse(Response response) {

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

    try {
        String jsonStr = response.body().string();
        jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString();
        return (ArrayList<Joke>) JSONParser.toObject(jsonStr,
                new TypeToken<ArrayList<Joke>>() {
                }.getType());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #6
Source File: Request4Joke.java    From JianDan with Apache License 2.0 6 votes vote down vote up
@Override
protected Response<ArrayList<Joke>> parseNetworkResponse(NetworkResponse response) {

	try {
		String jsonStr = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
		jsonStr = new JSONObject(jsonStr).getJSONArray("comments").toString();

		return Response.success((ArrayList<Joke>) JSONParser.toObject(jsonStr,
				new TypeToken<ArrayList<Joke>>() {
				}.getType()), HttpHeaderParser.parseCacheHeaders(response));

	} catch (Exception e) {
		e.printStackTrace();
		return Response.error(new ParseError(e));
	}
}
 
Example #7
Source File: JDApi.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
public static Observable<ArrayList<Joke>> getJokes(int page) {
    return Observable.create(new Observable.OnSubscribe<ArrayList<Joke>>() {
        @Override
        public void call(Subscriber<? super ArrayList<Joke>> subscriber) {
            try {
                subscriber.onNext(new JokeParser().parse(OkHttpProxy.get().url(Joke.getRequestUrl(page)).execute()));
                subscriber.onCompleted();
            } catch (IOException e) {
                e.printStackTrace();
                subscriber.onError(e);
            }
        }
    }).compose(applySchedulers());
}
 
Example #8
Source File: JokeCache.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
@Override
public ArrayList<Joke> getCacheByPage(int page) {
    QueryBuilder<com.socks.greendao.JokeCache> query = mJokeCacheDao.queryBuilder().where(JokeCacheDao.Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        return (ArrayList<Joke>) JSONParser.toObject(query.list().get(0).getResult(),
                new TypeToken<ArrayList<Joke>>() {
                }.getType());
    } else {
        return new ArrayList<>();
    }

}
 
Example #9
Source File: JokeAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
private void getCommentCounts(final ArrayList<Joke> jokes) {

        StringBuilder sb = new StringBuilder();
        for (Joke joke : jokes) {
            sb.append("comment-" + joke.getComment_ID() + ",");
        }

        String url = sb.toString();
        if (url.endsWith(",")) {
            url = url.substring(0, url.length() - 1);
        }

        JDApi.getCommentNumber(url)
                .observeOn(Schedulers.io())
                .doOnNext(commentNumbers -> {
                    if (page == 1) {
                        mJokes.clear();
                        JokeCache.getInstance(mActivity).clearAllCache();
                    }
                    JokeCache.getInstance(mActivity).addResultCache(GsonHelper.toString(jokes), page);
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(commentNumbers -> {
                    for (int i = 0; i < jokes.size(); i++) {
                        jokes.get(i).setComment_counts(commentNumbers.get(i).comments + "");
                    }
                    mJokes.addAll(jokes);
                    notifyDataSetChanged();
                    mLoadFinisCallBack.loadFinish(null);
                    mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                }, e -> {
                    mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET);
                    mLoadFinisCallBack.loadFinish(null);
                    KLog.e(e);
                });
    }
 
Example #10
Source File: JokeAdapter.java    From JianDanRxJava with Apache License 2.0 5 votes vote down vote up
@Override
public void onBindViewHolder(final JokeViewHolder holder, final int position) {

    final Joke joke = mJokes.get(position);
    holder.tv_content.setText(joke.getComment_content());
    holder.tv_author.setText(joke.getComment_author());
    holder.tv_time.setText(String2TimeUtil.dateString2GoodExperienceFormat(joke.getComment_date()));
    holder.tv_like.setText(joke.getVote_positive());
    holder.tv_comment_count.setText(joke.getComment_counts());
    holder.tv_unlike.setText(joke.getVote_negative());

    holder.img_share.setOnClickListener(v -> new MaterialDialog.Builder(mActivity)
            .items(R.array.joke_dialog)
            .backgroundColor(mActivity.getResources().getColor(JDApplication.COLOR_OF_DIALOG))
            .contentColor(JDApplication.COLOR_OF_DIALOG_CONTENT)
            .itemsCallback((dialog, view, which, text) -> {
                switch (which) {
                    case 0:
                        ShareUtil.shareText(mActivity, joke.getComment_content().trim());
                        break;
                    case 1:
                        TextUtil.copy(mActivity, joke.getComment_content());
                        break;
                }
            }).show());

    holder.ll_comment.setOnClickListener(v -> {
        Intent intent = new Intent(mActivity, CommentListActivity.class);
        intent.putExtra("thread_key", "comment-" + joke.getComment_ID());
        mActivity.startActivity(intent);
    });

    setAnimation(holder.card, position);

}
 
Example #11
Source File: JokeAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
private void loadData() {
    RequestManager.addRequest(new Request4Joke(Joke.getRequestUrl(page),
            new Response.Listener<ArrayList<Joke>>
                    () {
                @Override
                public void onResponse(ArrayList<Joke> response) {
                    getCommentCounts(response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mLoadFinisCallBack.loadFinish(null);
        }
    }), mActivity);
}
 
Example #12
Source File: JokeCache.java    From JianDan with Apache License 2.0 5 votes vote down vote up
@Override
public ArrayList<Joke> getCacheByPage(int page) {
    QueryBuilder<com.socks.greendao.JokeCache> query = mJokeCacheDao.queryBuilder().where(JokeCacheDao.Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        return (ArrayList<Joke>) JSONParser.toObject(query.list().get(0).getResult(),
                new TypeToken<ArrayList<Joke>>() {
                }.getType());
    } else {
        return new ArrayList<>();
    }

}
 
Example #13
Source File: JokeCache.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
@Override
public ArrayList<Joke> getCacheByPage(int page) {
    QueryBuilder<com.socks.greendao.JokeCache> query = mJokeCacheDao.queryBuilder().where(JokeCacheDao.Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        return (ArrayList<Joke>) JSONParser.toObject(query.list().get(0).getResult(),
                new TypeToken<ArrayList<Joke>>() {
                }.getType());
    } else {
        return new ArrayList<>();
    }

}
 
Example #14
Source File: JokeAdapter.java    From JianDan with Apache License 2.0 5 votes vote down vote up
private void loadData() {
    RequestManager.addRequest(new Request4Joke(Joke.getRequestUrl(page),
            new Response.Listener<ArrayList<Joke>>
                    () {
                @Override
                public void onResponse(ArrayList<Joke> response) {
                    getCommentCounts(response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mLoadFinisCallBack.loadFinish(null);
        }
    }), mActivity);
}
 
Example #15
Source File: JokeCache.java    From JianDan_OkHttpWithVolley with Apache License 2.0 5 votes vote down vote up
@Override
public ArrayList<Joke> getCacheByPage(int page) {
    QueryBuilder<com.socks.greendao.JokeCache> query = mJokeCacheDao.queryBuilder().where(JokeCacheDao.Properties.Page.eq("" + page));

    if (query.list().size() > 0) {
        return (ArrayList<Joke>) JSONParser.toObject(query.list().get(0).getResult(),
                new TypeToken<ArrayList<Joke>>() {
                }.getType());
    } else {
        return new ArrayList<>();
    }

}
 
Example #16
Source File: Request4Joke.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
public Request4Joke(String url, Response.Listener<ArrayList<Joke>> listener,
                    Response.ErrorListener errorListener) {
	super(Method.GET, url, errorListener);
	this.mListener = listener;
}
 
Example #17
Source File: Request4Joke.java    From JianDan_OkHttp with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Joke> response) {
	mListener.onResponse(response);
}
 
Example #18
Source File: Request4Joke.java    From JianDan with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Joke> response) {
	mListener.onResponse(response);
}
 
Example #19
Source File: Request4Joke.java    From JianDan with Apache License 2.0 4 votes vote down vote up
public Request4Joke(String url, Response.Listener<ArrayList<Joke>> listener,
                    Response.ErrorListener errorListener) {
	super(Method.GET, url, errorListener);
	this.mListener = listener;
}
 
Example #20
Source File: JokeAdapter.java    From JianDan with Apache License 2.0 4 votes vote down vote up
private void getCommentCounts(final ArrayList<Joke> jokes) {

        StringBuilder sb = new StringBuilder();
        for (Joke joke : jokes) {
            sb.append("comment-" + joke.getComment_ID() + ",");
        }

        String url = sb.toString();
        if (url.endsWith(",")) {
            url = url.substring(0, url.length() - 1);
        }

        RequestManager.addRequest(new Request4CommentCounts(CommentNumber.getCommentCountsURL(url), new Response
                .Listener<ArrayList<CommentNumber>>() {

            @Override
            public void onResponse(ArrayList<CommentNumber> response) {

                for (int i = 0; i < jokes.size(); i++) {
                    jokes.get(i).setComment_counts(response.get(i).getComments() + "");
                }

                if (page == 1) {
                    mJokes.clear();
                    //首次正常加载之后,清空之前的缓存
                    JokeCache.getInstance(mActivity).clearAllCache();
                }

                mJokes.addAll(jokes);
                notifyDataSetChanged();

                //加载完毕后缓存
                JokeCache.getInstance(mActivity).addResultCache(JSONParser.toString(jokes),
                        page);
                mLoadFinisCallBack.loadFinish(null);
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                mLoadFinisCallBack.loadFinish(null);
            }
        }
        ), mActivity);

    }
 
Example #21
Source File: Request4Joke.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Joke> response) {
	mListener.onResponse(response);
}
 
Example #22
Source File: Request4Joke.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
public Request4Joke(String url, Response.Listener<ArrayList<Joke>> listener,
                    Response.ErrorListener errorListener) {
	super(Method.GET, url, errorListener);
	this.mListener = listener;
}
 
Example #23
Source File: JokeAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
private void getCommentCounts(final ArrayList<Joke> jokes) {

        StringBuilder sb = new StringBuilder();
        for (Joke joke : jokes) {
            sb.append("comment-" + joke.getComment_ID() + ",");
        }

        String url = sb.toString();
        if (url.endsWith(",")) {
            url = url.substring(0, url.length() - 1);
        }

        RequestManager.addRequest(new Request4CommentCounts(CommentNumber.getCommentCountsURL(url), new Response
                .Listener<ArrayList<CommentNumber>>() {

            @Override
            public void onResponse(ArrayList<CommentNumber> response) {

                for (int i = 0; i < jokes.size(); i++) {
                    jokes.get(i).setComment_counts(response.get(i).getComments() + "");
                }

                if (page == 1) {
                    mJokes.clear();
                    //首次正常加载之后,清空之前的缓存
                    JokeCache.getInstance(mActivity).clearAllCache();
                }

                mJokes.addAll(jokes);
                notifyDataSetChanged();

                //加载完毕后缓存
                JokeCache.getInstance(mActivity).addResultCache(JSONParser.toString(jokes),
                        page);
                mLoadFinisCallBack.loadFinish(null);
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                mLoadFinisCallBack.loadFinish(null);
            }
        }
        ), mActivity);

    }