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

The following examples show how to use com.socks.jiandan.model.Joke. 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: Request4Joke.java    License: 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 2
Source Project: JianDan   Source File: Request4Joke.java    License: 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 Project: JianDanRxJava   Source File: JokeAdapter.java    License: 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 4
Source Project: JianDanRxJava   Source File: JokeParser.java    License: 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 5
Source Project: JianDan_OkHttp   Source File: JokeParser.java    License: 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 Project: JianDan_OkHttp   Source File: Request4Joke.java    License: 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 Project: JianDan_OkHttpWithVolley   Source File: JokeAdapter.java    License: 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 8
Source Project: JianDan_OkHttpWithVolley   Source File: JokeCache.java    License: 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 Project: JianDan   Source File: JokeAdapter.java    License: 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 10
Source Project: JianDan   Source File: JokeCache.java    License: 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 11
Source Project: JianDanRxJava   Source File: JokeAdapter.java    License: 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 12
Source Project: JianDanRxJava   Source File: JokeAdapter.java    License: 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 13
Source Project: JianDanRxJava   Source File: JokeCache.java    License: 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 Project: JianDanRxJava   Source File: JDApi.java    License: 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 15
Source Project: JianDan_OkHttp   Source File: JokeCache.java    License: 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 Project: JianDan_OkHttpWithVolley   Source File: JokeAdapter.java    License: 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 17
Source Project: JianDan_OkHttpWithVolley   Source File: Request4Joke.java    License: 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 18
Source Project: JianDan_OkHttpWithVolley   Source File: Request4Joke.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Joke> response) {
	mListener.onResponse(response);
}
 
Example 19
Source Project: JianDan   Source File: JokeAdapter.java    License: 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 20
Source Project: JianDan   Source File: Request4Joke.java    License: 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 21
Source Project: JianDan   Source File: Request4Joke.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Joke> response) {
	mListener.onResponse(response);
}
 
Example 22
Source Project: JianDan_OkHttp   Source File: Request4Joke.java    License: 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 Project: JianDan_OkHttp   Source File: Request4Joke.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected void deliverResponse(ArrayList<Joke> response) {
	mListener.onResponse(response);
}