Android Open Source - doubanbook4android Douban Business






From Project

Back to project page doubanbook4android.

License

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.

Java Source Code

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;
  }

}




Java Source Code List

.UserListAdapter.java
com.study.doubanbook_for_android.activity.BaseActivity.java
com.study.doubanbook_for_android.activity.BaseP2RActivity.java
com.study.doubanbook_for_android.activity.BookDetailActivity.java
com.study.doubanbook_for_android.activity.BookListsActivity.java
com.study.doubanbook_for_android.activity.BookNoteListActivity.java
com.study.doubanbook_for_android.activity.CollectDetailActivity.java
com.study.doubanbook_for_android.activity.NoteAndUserDetailActivity.java
com.study.doubanbook_for_android.activity.SerchInputActivity.java
com.study.doubanbook_for_android.activity.TextActivity1.java
com.study.doubanbook_for_android.activity.UserDetailActivity.java
com.study.doubanbook_for_android.activity.UserListActivity.java
com.study.doubanbook_for_android.activity.UserNoteActivity.java
com.study.doubanbook_for_android.adapter.BookAdapter.java
com.study.doubanbook_for_android.adapter.CommentAdapter.java
com.study.doubanbook_for_android.adapter.UserNoteAdapter.java
com.study.doubanbook_for_android.api.NetUtils.java
com.study.doubanbook_for_android.api.SSLSocketFactoryEx.java
com.study.doubanbook_for_android.api.WrongMsg.java
com.study.doubanbook_for_android.auth.AccessToken.java
com.study.doubanbook_for_android.auth.DoubanDialogError.java
com.study.doubanbook_for_android.auth.DoubanDialog.java
com.study.doubanbook_for_android.auth.DoubanException.java
com.study.doubanbook_for_android.auth.DoubanOAuthListener.java
com.study.doubanbook_for_android.auth.DoubanParameters.java
com.study.doubanbook_for_android.auth.DoubanRequest.java
com.study.doubanbook_for_android.auth.Douban.java
com.study.doubanbook_for_android.auth.KeepToken.java
com.study.doubanbook_for_android.auth.SimpleDoubanOAuthListener.java
com.study.doubanbook_for_android.auth.Token.java
com.study.doubanbook_for_android.business.DoubanBusiness.java
com.study.doubanbook_for_android.callback.AsynCallback.java
com.study.doubanbook_for_android.imagedownloader.FileCache.java
com.study.doubanbook_for_android.imagedownloader.INetImageDownloader.java
com.study.doubanbook_for_android.imagedownloader.ImageCache.java
com.study.doubanbook_for_android.imagedownloader.ImageDownloaderListener.java
com.study.doubanbook_for_android.imagedownloader.ImageDownloader.java
com.study.doubanbook_for_android.model.Annotations.java
com.study.doubanbook_for_android.model.AuthorUser.java
com.study.doubanbook_for_android.model.BookItem.java
com.study.doubanbook_for_android.model.CollectBookMsg.java
com.study.doubanbook_for_android.model.CollectSuccessResult.java
com.study.doubanbook_for_android.model.CommentItem.java
com.study.doubanbook_for_android.model.Comment.java
com.study.doubanbook_for_android.model.CurrenUserCollection.java
com.study.doubanbook_for_android.model.GeneralNoteResult.java
com.study.doubanbook_for_android.model.GeneralResult.java
com.study.doubanbook_for_android.model.GeneralUserResult.java
com.study.doubanbook_for_android.model.ImageItem.java
com.study.doubanbook_for_android.model.Rating.java
com.study.doubanbook_for_android.model.RequestGrantScope.java
com.study.doubanbook_for_android.model.TagItem.java
com.study.doubanbook_for_android.model.URLMananeger.java
com.study.doubanbook_for_android.utils.EncodeUtils.java
com.study.doubanbook_for_android.utils.JsonParser.java
com.study.doubanbook_for_android.utils.JsonUtil.java
com.study.doubanbook_for_android.utils.UriUtils.java