Back to project page doubanbook4android.
The source code is released under:
Eclipse Public License - v 1.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECI...
If you think the Android project doubanbook4android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.study.doubanbook_for_android.business; /*from w w w. j av a2 s . co m*/ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.study.doubanbook_for_android.activity.BaseActivity; import com.study.doubanbook_for_android.api.NetUtils; import com.study.doubanbook_for_android.api.WrongMsg; import com.study.doubanbook_for_android.auth.Douban; import com.study.doubanbook_for_android.auth.SimpleDoubanOAuthListener; import com.study.doubanbook_for_android.callback.AsynCallback; import com.study.doubanbook_for_android.model.CollectBookMsg; import com.study.doubanbook_for_android.model.CollectSuccessResult; import com.study.doubanbook_for_android.model.GeneralNoteResult; import com.study.doubanbook_for_android.model.GeneralResult; import com.study.doubanbook_for_android.model.GeneralUserResult; import com.study.doubanbook_for_android.model.URLMananeger; public class DoubanBusiness { private Context context; public DoubanBusiness(Context context) { super(); this.context = context; } public void auth(){ Douban douban = Douban.getInstance(); douban.authorize(context, new SimpleDoubanOAuthListener()); } // TODO ??????,????,??,?? DONE // ?????,????MODEL???TRY CATCH,???????????????,??????WRONGMSG // MODEL???CODE???TAG,CODE==0????????????????WRONGMSG /** * q ????? q?tag???? * tag ???tag q?tag???? * start ??????offset ???0 * count ?????????? * ???20????100 * * @param q * @param start * @param callback */ public void getSearchList(final String q, final int start, final AsynCallback<GeneralResult> callback) { new Thread() { public void run() { WrongMsg wrongMsg = new WrongMsg(); GeneralResult result = null; Gson gson = new Gson(); String s = ""; List<String> keys = new ArrayList<String>(); List<String> values = new ArrayList<String>(); // init keys keys.add("q"); keys.add("start"); keys.add("count"); // init values try { // change the encode when necessary values.add(URLEncoder.encode(q, "UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } values.add(start + ""); values.add(BaseActivity.PAGE_COUNT + ""); callback.onStart(); s = NetUtils.getHttpEntity( getBasicUrl(URLMananeger.BOOK_WRITER_SEARCHR_URL), NetUtils.GET, keys, values, context); wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() { }.getType()); if (wrongMsg.getCode() != 0) { Log.d("NET", "wrongmsg model"); callback.onFailure(wrongMsg); } else { Log.d("NET", "right model"); result = gson.fromJson(s, new TypeToken<GeneralResult>() { }.getType()); callback.onSuccess(result); } callback.onDone(); }; }.start(); } /** * format ??content????? ????????????text, HTML????html?????text * order ??? * ????????collect, ??????rank, ?????????page?????rank * page ?????? ?? * * @param bookid * @param start * @param callback */ public void getNoteList(final String bookid, final int start, final AsynCallback<GeneralNoteResult> callback) { new Thread() { public void run() { WrongMsg wrongMsg = new WrongMsg(); GeneralNoteResult result = null; Gson gson = new Gson(); String s = ""; List<String> keys = new ArrayList<String>(); List<String> values = new ArrayList<String>(); // init keys keys.add("start"); keys.add("count"); // init values values.add(start + ""); values.add(BaseActivity.PAGE_COUNT + ""); callback.onStart(); s = NetUtils.getHttpEntity( getBasicUrl(URLMananeger.BOOK_NOTE_LIST_URL.replace( ":id", bookid)), NetUtils.GET, keys, values, null); wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() { }.getType()); if (wrongMsg.getCode() != 0) { Log.d("NET", "wrongmsg model"); callback.onFailure(wrongMsg); } else { Log.d("NET", "right model"); result = gson.fromJson(s, new TypeToken<GeneralNoteResult>() { }.getType()); callback.onSuccess(result); } callback.onDone(); }; }.start(); } /** * * ????????????????? * * POST https://api.douban.com/v2/book/:id/collection ???? ??? ?? status ?????? ??????wish ???reading ? doing ???read ? done? tags ???????? ???????? comment ???? ?????350? privacy ?????? ?????'private'????????????????????? rating ?? ?????1?5???????????????????? * @param bookid * @param start * @param callback */ //TODO NEED TEST when success show toast public void collectBook(final String bookid,final CollectBookMsg collectmsg, final AsynCallback<CollectSuccessResult> callback) { new Thread() { public void run() { WrongMsg wrongMsg = new WrongMsg(); CollectSuccessResult result = null; Gson gson = new Gson(); String s = ""; List<String> keys = new ArrayList<String>(); List<String> values = new ArrayList<String>(); keys.add("status");//NOT NULL values.add(collectmsg.getStatus()); //TODO NOT USE YET //keys.add("tags"); if(collectmsg.getComment()!=null){ values.add(collectmsg.getComment()); keys.add("comment"); } if(collectmsg.getPrivacy()){ keys.add("privacy"); values.add("private"); } if(collectmsg.getRating()>=1&&collectmsg.getRating()<=5){ keys.add("rating"); values.add(String.valueOf(collectmsg.getRating())); } callback.onStart(); s = NetUtils.getHttpEntity( getBasicUrl(URLMananeger.BOOK_COLLECT_URL.replace( ":id", bookid)), NetUtils.POST, keys, values, context); wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() { }.getType()); if (wrongMsg.getCode() != 0) { Log.d("NET", "wrongmsg model"); callback.onFailure(wrongMsg); } else { Log.d("NET", "right model"); result = gson.fromJson(s, new TypeToken<CollectSuccessResult>() { }.getType()); callback.onSuccess(result); } callback.onDone(); }; }.start(); } /** * GET https://api.douban.com/v2/book/user/:name/collections ???? ??? ?? status ?????? ??????wish ???reading ???read????????? tag ????? ?? from ??????????????? ???????????rfc3339???????"2012-10-19T17:14:11"??????????????? to ????????????????? ???? rating ?? ?????1?5?????????????????????? * @param bookid * @param collectmsg * @param start * @param callback */ public void getUserCollection(final String bookid,final CollectBookMsg collectmsg, final int start, final AsynCallback<CollectSuccessResult> callback) { new Thread() { public void run() { WrongMsg wrongMsg = new WrongMsg(); CollectSuccessResult result = null; Gson gson = new Gson(); String s = ""; List<String> keys = new ArrayList<String>(); List<String> values = new ArrayList<String>(); // init keys // init values keys.add("status"); values.add(collectmsg.getStatus()); //TODO NOT USE YET //keys.add("tags"); if(collectmsg.getComment()!=null){ values.add(collectmsg.getComment()); keys.add("comment"); } if(collectmsg.getPrivacy()){ keys.add("privacy"); values.add("private"); } if(collectmsg.getRating()>=1&&collectmsg.getRating()<=5){ keys.add("rating"); values.add(String.valueOf(collectmsg.getRating())); } callback.onStart(); s = NetUtils.getHttpEntity( getBasicUrl(URLMananeger.BOOK_COLLECT_URL.replace( ":name", bookid)), NetUtils.POST, keys, values, context); wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() { }.getType()); if (wrongMsg.getCode() != 0) { Log.d("NET", "wrongmsg model"); callback.onFailure(wrongMsg); } else { Log.d("NET", "right model"); result = gson.fromJson(s, new TypeToken<CollectSuccessResult>() { }.getType()); callback.onSuccess(result); } callback.onDone(); }; }.start(); } //TODO NOT FIND TEST BY POSTMAN DONE BY 2013-12-20-20:39 /** * GET https://api.douban.com/v2/user ???? ??? ?? q ????? q?tag???? tag ???tag q?tag???? start ??????offset ???0 count ?????????? ???20????100 * @param userName * @param collectmsg * @param start * @param callback */ public void searchUser(final String userName,final int start, final AsynCallback<GeneralUserResult> callback) { new Thread() { public void run() { WrongMsg wrongMsg = new WrongMsg(); GeneralUserResult result = null; Gson gson = new Gson(); String s = ""; List<String> keys = new ArrayList<String>(); List<String> values = new ArrayList<String>(); // init keys keys.add("q"); keys.add("start"); keys.add("count"); // init values try { // change the encode when necessary values.add(URLEncoder.encode(userName, "UTF-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } values.add(start + ""); values.add(BaseActivity.PAGE_COUNT + ""); callback.onStart(); s = NetUtils.getHttpEntity( getBasicUrl(URLMananeger.USER_SEARCH_URL), NetUtils.GET, keys, values, null); wrongMsg = gson.fromJson(s, new TypeToken<WrongMsg>() { }.getType()); if (wrongMsg.getCode() != 0) { Log.d("NET", "wrongmsg model"); callback.onFailure(wrongMsg); } else { Log.d("NET", "right model"); result = gson.fromJson(s, new TypeToken<GeneralUserResult>() { }.getType()); callback.onSuccess(result); } callback.onDone(); }; }.start(); } /** * get request url * * @param url * @return */ private static String getBasicUrl(String url) { return URLMananeger.ROOT_ULR + url; } }