parent
de70b8aa95
commit
365b261ee0
@ -1,313 +0,0 @@ |
|||||||
package com.novel.read.http; |
|
||||||
|
|
||||||
import android.content.Context; |
|
||||||
import android.text.TextUtils; |
|
||||||
import android.util.Log; |
|
||||||
|
|
||||||
import com.mango.mangolib.event.EventManager; |
|
||||||
import com.mango.mangolib.http.ErrorResponse; |
|
||||||
import com.mango.mangolib.http.MyRequestType; |
|
||||||
import com.mango.mangolib.http.ServiceCallback; |
|
||||||
import com.novel.read.event.AddBookSignEvent; |
|
||||||
import com.novel.read.event.BookArticleEvent; |
|
||||||
import com.novel.read.event.DeleteBookSignEvent; |
|
||||||
import com.novel.read.event.ErrorChapterEvent; |
|
||||||
import com.novel.read.event.FinishChapterEvent; |
|
||||||
import com.novel.read.event.GetBookDetailEvent; |
|
||||||
import com.novel.read.event.GetBookSignEvent; |
|
||||||
import com.novel.read.event.GetCategoryTypeEvent; |
|
||||||
import com.novel.read.event.GetRecommendBookEvent; |
|
||||||
import com.novel.read.event.HotSearchEvent; |
|
||||||
import com.novel.read.event.LoginEvent; |
|
||||||
import com.novel.read.event.SearchListEvent; |
|
||||||
import com.novel.read.event.VersionEvent; |
|
||||||
import com.novel.read.http.service.AccountService; |
|
||||||
import com.novel.read.model.db.ChapterInfoBean; |
|
||||||
import com.novel.read.model.db.dbManage.BookRepository; |
|
||||||
import com.novel.read.model.protocol.BookArticleResp; |
|
||||||
import com.novel.read.model.protocol.BookDetailResp; |
|
||||||
import com.novel.read.model.protocol.CategoryTypeResp; |
|
||||||
import com.novel.read.model.protocol.HotSearchResp; |
|
||||||
import com.novel.read.model.protocol.MarkResp; |
|
||||||
import com.novel.read.model.protocol.RankByUpadateResp; |
|
||||||
import com.novel.read.model.protocol.RecommendBookResp; |
|
||||||
import com.novel.read.model.protocol.RecommendListResp; |
|
||||||
import com.novel.read.model.protocol.SearchResp; |
|
||||||
import com.novel.read.model.protocol.UidResp; |
|
||||||
import com.novel.read.model.protocol.VersionResp; |
|
||||||
import com.novel.read.utlis.LogUtils; |
|
||||||
import com.novel.read.utlis.PhoneUtils; |
|
||||||
import com.novel.read.widget.page.TxtChapter; |
|
||||||
|
|
||||||
import org.reactivestreams.Subscriber; |
|
||||||
import org.reactivestreams.Subscription; |
|
||||||
|
|
||||||
import java.util.ArrayDeque; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.HashMap; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
import io.reactivex.Single; |
|
||||||
import io.reactivex.android.schedulers.AndroidSchedulers; |
|
||||||
import io.reactivex.schedulers.Schedulers; |
|
||||||
import okhttp3.MediaType; |
|
||||||
import okhttp3.RequestBody; |
|
||||||
import retrofit2.Call; |
|
||||||
import retrofit2.Callback; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by zlj on 2017/2/14. |
|
||||||
*/ |
|
||||||
|
|
||||||
public class AccountManager { |
|
||||||
|
|
||||||
private AccountService accountService; |
|
||||||
|
|
||||||
private AccountManager() { |
|
||||||
accountService = ServiceGenerator.createService(AccountService.class, MyRequestType.URL_TEXT); |
|
||||||
} |
|
||||||
|
|
||||||
private static AccountManager instance = null; |
|
||||||
|
|
||||||
public static synchronized AccountManager getInstance() { |
|
||||||
if (instance == null) { |
|
||||||
instance = new AccountManager(); |
|
||||||
} |
|
||||||
return instance; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public void getRecommendBook(String bookId, String limit) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("book_id", bookId); |
|
||||||
Call<RecommendBookResp> call = accountService.getRecommendBook(getUrlString(Urls.getRecommend, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(GetRecommendBookEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void getBookDetail(String bookId) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("book_id", bookId); |
|
||||||
Call<BookDetailResp> call = accountService.getBookDetail(getUrlString(Urls.getBookDetail, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(GetBookDetailEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public Single<BookDetailResp> getBookDetails(String bookId) { //rxjava 获取多书籍详情 合并请求调用
|
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("book_id", bookId); |
|
||||||
return accountService.getBookDetails(getUrlString(Urls.getBookDetail, map)); |
|
||||||
} |
|
||||||
|
|
||||||
public void getCategoryType() { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
Call<CategoryTypeResp> call = accountService.getCategoryType(getUrlString(Urls.getCategoryType, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(GetCategoryTypeEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void getHotSearch() { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
Call<HotSearchResp> call = accountService.getHotSearch(getUrlString(Urls.getHotSearch, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(HotSearchEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void getBookArticle(String bookId, String hasContent, String page, String limit) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("book_id", bookId); |
|
||||||
map.put("has_content", hasContent); |
|
||||||
map.put("page", page); |
|
||||||
map.put("limit", limit); |
|
||||||
Call<BookArticleResp> call = accountService.getBookArticle(getUrlString(Urls.getBookArticle, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(BookArticleEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public void getSearchBookList(String category_id, String key, int page) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
if (!TextUtils.isEmpty(category_id)) { |
|
||||||
map.put("category_id", category_id); |
|
||||||
} else { |
|
||||||
map.put("category_id", "0"); |
|
||||||
} |
|
||||||
if (!TextUtils.isEmpty(key)) { |
|
||||||
map.put("key", key); |
|
||||||
} |
|
||||||
map.put("page", String.valueOf(page)); |
|
||||||
Call<SearchResp> call = accountService.getSearchList(getUrlString(Urls.getBookList, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(SearchListEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void getRecommendList(String listType, Callback<RecommendListResp> callback) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("type", listType); |
|
||||||
Call<RecommendListResp> call = accountService.getRecommendList(getUrlString(Urls.getRecommendList, map)); |
|
||||||
call.enqueue(callback); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
public void getRankByUpdate(int page, int limit, Callback<RankByUpadateResp> callback) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
if (!TextUtils.isEmpty(String.valueOf(page))) { |
|
||||||
map.put("page", String.valueOf(page)); |
|
||||||
} |
|
||||||
if (limit != 0) { |
|
||||||
map.put("limit", String.valueOf(limit)); |
|
||||||
} |
|
||||||
Call<RankByUpadateResp> call = accountService.getRankByUpdate(getUrlString(Urls.getRankByUpdate, map)); |
|
||||||
call.enqueue(callback); |
|
||||||
} |
|
||||||
|
|
||||||
public void getRankList(String type, String sex, String dateType, String page, Callback<RankByUpadateResp> callback) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("type", type); |
|
||||||
map.put("gender", sex); |
|
||||||
map.put("date_type", dateType); |
|
||||||
map.put("page", page); |
|
||||||
Call<RankByUpadateResp> call = accountService.getRankList(getUrlString(Urls.getRankList, map)); |
|
||||||
call.enqueue(callback); |
|
||||||
} |
|
||||||
|
|
||||||
public void checkVersion(int versionCode) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("version", String.valueOf(versionCode)); |
|
||||||
// map.put("shell", Constant.shell);
|
|
||||||
Call<VersionResp> call = accountService.checkVersion(getUrlString(Urls.checkVersion, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(VersionEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void addSign(String bookId, String articleId, String content) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("book_id", bookId); |
|
||||||
map.put("article_id", articleId); |
|
||||||
map.put("content", content); |
|
||||||
Call<ErrorResponse> call = accountService.addSign(Urls.addBookSign, mapToBody(map)); |
|
||||||
call.enqueue(new ServiceCallback<>(AddBookSignEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void deleteSign(String signIds) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("sign_ids", signIds); |
|
||||||
Call<ErrorResponse> call = accountService.deleteSign(Urls.deleteSign, mapToBody(map)); |
|
||||||
call.enqueue(new ServiceCallback<>(DeleteBookSignEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void getSignList(String bookId) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("book_id", bookId); |
|
||||||
Call<MarkResp> call = accountService.getSignList(getUrlString(Urls.getBookSign, map)); |
|
||||||
call.enqueue(new ServiceCallback<>(GetBookSignEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
public void login(Context mContext) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
Log.e("getUniquePsuedoID", "login: " + PhoneUtils.getUniquePsuedoID()); |
|
||||||
map.put("code", PhoneUtils.getUniquePsuedoID()); |
|
||||||
Call<UidResp> call = accountService.login(Urls.login, mapToBody(map)); |
|
||||||
call.enqueue(new ServiceCallback<>(LoginEvent.class)); |
|
||||||
} |
|
||||||
|
|
||||||
private Subscription mChapterSub; |
|
||||||
|
|
||||||
public void getBookArticleDetail(String bookId, List<TxtChapter> bookChapters) { |
|
||||||
int size = bookChapters.size(); |
|
||||||
//取消上次的任务,防止多次加载
|
|
||||||
if (mChapterSub != null) { |
|
||||||
mChapterSub.cancel(); |
|
||||||
} |
|
||||||
List<Single<ChapterInfoBean>> chapterInfos = new ArrayList<>(bookChapters.size()); |
|
||||||
ArrayDeque<String> titles = new ArrayDeque<>(bookChapters.size()); |
|
||||||
|
|
||||||
// 将要下载章节,转换成网络请求。
|
|
||||||
for (int i = 0; i < size; ++i) { |
|
||||||
TxtChapter bookChapter = bookChapters.get(i); |
|
||||||
// 网络中获取数据
|
|
||||||
Single<ChapterInfoBean> chapterInfoSingle = getChapterInfo(bookChapter.getChapterId()); |
|
||||||
chapterInfos.add(chapterInfoSingle); |
|
||||||
titles.add(bookChapter.getTitle()); |
|
||||||
} |
|
||||||
|
|
||||||
Single.concat(chapterInfos) |
|
||||||
.subscribeOn(Schedulers.io()) |
|
||||||
.observeOn(AndroidSchedulers.mainThread()) |
|
||||||
.subscribe(new Subscriber<ChapterInfoBean>() { |
|
||||||
String title = titles.poll(); |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onSubscribe(Subscription s) { |
|
||||||
s.request(Integer.MAX_VALUE); |
|
||||||
mChapterSub = s; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onNext(ChapterInfoBean chapterInfoBean) { |
|
||||||
//存储数据
|
|
||||||
BookRepository.getInstance().saveChapterInfo( |
|
||||||
bookId, title, chapterInfoBean.getBody() |
|
||||||
); |
|
||||||
EventManager.Companion.getInstance().postEvent(new FinishChapterEvent()); |
|
||||||
//将获取到的数据进行存储
|
|
||||||
title = titles.poll(); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onError(Throwable t) { |
|
||||||
//只有第一个加载失败才会调用errorChapter
|
|
||||||
if (bookChapters.get(0).getTitle().equals(title)) { |
|
||||||
EventManager.Companion.getInstance().postEvent(new ErrorChapterEvent()); |
|
||||||
} |
|
||||||
LogUtils.e(t); |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public void onComplete() { |
|
||||||
} |
|
||||||
} |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 注意这里用的是同步请求 |
|
||||||
*/ |
|
||||||
public Single<ChapterInfoBean> getChapterInfo(String id) { |
|
||||||
HashMap<String, String> map = new HashMap<>(); |
|
||||||
map.put("article_id", id); |
|
||||||
return accountService.getBookArticleDetail(getUrlString(Urls.getDetail, map)) |
|
||||||
.map(bean -> bean.getArticle().get(0)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* get方法拼接字符串 |
|
||||||
*/ |
|
||||||
private String getUrlString(String path, HashMap<String, String> query) { |
|
||||||
String mypath = path; |
|
||||||
if (query != null && query.size() > 0) { |
|
||||||
StringBuilder pathWithQuery = new StringBuilder(path); |
|
||||||
if (!path.contains("?")) { |
|
||||||
pathWithQuery.append("?"); |
|
||||||
} else { |
|
||||||
pathWithQuery.append("&"); |
|
||||||
} |
|
||||||
|
|
||||||
for (Map.Entry<String, String> stringStringEntry : query.entrySet()) { |
|
||||||
String key = (String) ((Map.Entry) stringStringEntry).getKey(); |
|
||||||
String val = (String) ((Map.Entry) stringStringEntry).getValue(); |
|
||||||
pathWithQuery.append(key); |
|
||||||
pathWithQuery.append("="); |
|
||||||
pathWithQuery.append(val); |
|
||||||
pathWithQuery.append("&"); |
|
||||||
} |
|
||||||
|
|
||||||
pathWithQuery.deleteCharAt(pathWithQuery.length() - 1); |
|
||||||
mypath = pathWithQuery.toString(); |
|
||||||
} |
|
||||||
|
|
||||||
return mypath; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 减少请求info类的数量,直接用map替代实体类,返回body |
|
||||||
*/ |
|
||||||
private RequestBody mapToBody(HashMap<String, String> map) { |
|
||||||
return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), ServiceGenerator.formatResponse(map)); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,309 @@ |
|||||||
|
package com.novel.read.http |
||||||
|
|
||||||
|
import android.content.Context |
||||||
|
import android.text.TextUtils |
||||||
|
import android.util.Log |
||||||
|
|
||||||
|
import com.mango.mangolib.event.EventManager |
||||||
|
import com.mango.mangolib.http.MyRequestType |
||||||
|
import com.mango.mangolib.http.ServiceCallback |
||||||
|
import com.novel.read.event.AddBookSignEvent |
||||||
|
import com.novel.read.event.BookArticleEvent |
||||||
|
import com.novel.read.event.DeleteBookSignEvent |
||||||
|
import com.novel.read.event.ErrorChapterEvent |
||||||
|
import com.novel.read.event.FinishChapterEvent |
||||||
|
import com.novel.read.event.GetBookDetailEvent |
||||||
|
import com.novel.read.event.GetBookSignEvent |
||||||
|
import com.novel.read.event.GetCategoryTypeEvent |
||||||
|
import com.novel.read.event.GetRecommendBookEvent |
||||||
|
import com.novel.read.event.HotSearchEvent |
||||||
|
import com.novel.read.event.LoginEvent |
||||||
|
import com.novel.read.event.SearchListEvent |
||||||
|
import com.novel.read.event.VersionEvent |
||||||
|
import com.novel.read.http.service.AccountService |
||||||
|
import com.novel.read.model.db.ChapterInfoBean |
||||||
|
import com.novel.read.model.db.dbManage.BookRepository |
||||||
|
import com.novel.read.model.protocol.BookDetailResp |
||||||
|
import com.novel.read.model.protocol.RankByUpadateResp |
||||||
|
import com.novel.read.model.protocol.RecommendListResp |
||||||
|
import com.novel.read.utlis.LogUtils |
||||||
|
import com.novel.read.utlis.PhoneUtils |
||||||
|
import com.novel.read.widget.page.TxtChapter |
||||||
|
|
||||||
|
import org.reactivestreams.Subscriber |
||||||
|
import org.reactivestreams.Subscription |
||||||
|
|
||||||
|
import java.util.ArrayDeque |
||||||
|
import java.util.ArrayList |
||||||
|
import java.util.HashMap |
||||||
|
|
||||||
|
import io.reactivex.Single |
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers |
||||||
|
import io.reactivex.schedulers.Schedulers |
||||||
|
import okhttp3.MediaType |
||||||
|
import okhttp3.RequestBody |
||||||
|
import retrofit2.Callback |
||||||
|
import java.util.Map |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by zlj on 2017/2/14. |
||||||
|
*/ |
||||||
|
|
||||||
|
class AccountManager private constructor() { |
||||||
|
|
||||||
|
private val accountService: AccountService |
||||||
|
|
||||||
|
private var mChapterSub: Subscription? = null |
||||||
|
|
||||||
|
init { |
||||||
|
accountService = ServiceGenerator.createService(AccountService::class.java, MyRequestType.URL_TEXT) |
||||||
|
} |
||||||
|
|
||||||
|
fun getRecommendBook(bookId: String, limit: String) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["book_id"] = bookId |
||||||
|
val call = accountService.getRecommendBook(getUrlString(Urls.getRecommend, map)) |
||||||
|
call.enqueue(ServiceCallback(GetRecommendBookEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getBookDetail(bookId: String) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["book_id"] = bookId |
||||||
|
val call = accountService.getBookDetail(getUrlString(Urls.getBookDetail, map)) |
||||||
|
call.enqueue(ServiceCallback(GetBookDetailEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getBookDetails(bookId: String): Single<BookDetailResp> { //rxjava 获取多书籍详情 合并请求调用 |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["book_id"] = bookId |
||||||
|
return accountService.getBookDetails(getUrlString(Urls.getBookDetail, map)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getCategoryType() { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
val call = accountService.getCategoryType(getUrlString(Urls.getCategoryType, map)) |
||||||
|
call.enqueue(ServiceCallback(GetCategoryTypeEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getHotSearch() { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
val call = accountService.getHotSearch(getUrlString(Urls.getHotSearch, map)) |
||||||
|
call.enqueue(ServiceCallback(HotSearchEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getBookArticle(bookId: String, hasContent: String, page: String, limit: String) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["book_id"] = bookId |
||||||
|
map["has_content"] = hasContent |
||||||
|
map["page"] = page |
||||||
|
map["limit"] = limit |
||||||
|
val call = accountService.getBookArticle(getUrlString(Urls.getBookArticle, map)) |
||||||
|
call.enqueue(ServiceCallback(BookArticleEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
fun getSearchBookList(category_id: String, key: String, page: Int) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
if (!TextUtils.isEmpty(category_id)) { |
||||||
|
map["category_id"] = category_id |
||||||
|
} else { |
||||||
|
map["category_id"] = "0" |
||||||
|
} |
||||||
|
if (!TextUtils.isEmpty(key)) { |
||||||
|
map["key"] = key |
||||||
|
} |
||||||
|
map["page"] = page.toString() |
||||||
|
val call = accountService.getSearchList(getUrlString(Urls.getBookList, map)) |
||||||
|
call.enqueue(ServiceCallback(SearchListEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getRecommendList(listType: String, callback: Callback<RecommendListResp>) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["type"] = listType |
||||||
|
val call = accountService.getRecommendList(getUrlString(Urls.getRecommendList, map)) |
||||||
|
call.enqueue(callback) |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
fun getRankByUpdate(page: Int, limit: Int, callback: Callback<RankByUpadateResp>) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
if (!TextUtils.isEmpty(page.toString())) { |
||||||
|
map["page"] = page.toString() |
||||||
|
} |
||||||
|
if (limit != 0) { |
||||||
|
map["limit"] = limit.toString() |
||||||
|
} |
||||||
|
val call = accountService.getRankByUpdate(getUrlString(Urls.getRankByUpdate, map)) |
||||||
|
call.enqueue(callback) |
||||||
|
} |
||||||
|
|
||||||
|
fun getRankList( |
||||||
|
type: String, |
||||||
|
sex: String, |
||||||
|
dateType: String, |
||||||
|
page: String, |
||||||
|
callback: Callback<RankByUpadateResp> |
||||||
|
) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["type"] = type |
||||||
|
map["gender"] = sex |
||||||
|
map["date_type"] = dateType |
||||||
|
map["page"] = page |
||||||
|
val call = accountService.getRankList(getUrlString(Urls.getRankList, map)) |
||||||
|
call.enqueue(callback) |
||||||
|
} |
||||||
|
|
||||||
|
fun checkVersion(versionCode: Int) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["version"] = versionCode.toString() |
||||||
|
// map.put("shell", Constant.shell); |
||||||
|
val call = accountService.checkVersion(getUrlString(Urls.checkVersion, map)) |
||||||
|
call.enqueue(ServiceCallback(VersionEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun addSign(bookId: String, articleId: String, content: String) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["book_id"] = bookId |
||||||
|
map["article_id"] = articleId |
||||||
|
map["content"] = content |
||||||
|
val call = accountService.addSign(Urls.addBookSign, mapToBody(map)) |
||||||
|
call.enqueue(ServiceCallback(AddBookSignEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun deleteSign(signIds: String) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["sign_ids"] = signIds |
||||||
|
val call = accountService.deleteSign(Urls.deleteSign, mapToBody(map)) |
||||||
|
call.enqueue(ServiceCallback(DeleteBookSignEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getSignList(bookId: String) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["book_id"] = bookId |
||||||
|
val call = accountService.getSignList(getUrlString(Urls.getBookSign, map)) |
||||||
|
call.enqueue(ServiceCallback(GetBookSignEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun login(mContext: Context) { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
Log.e("getUniquePsuedoID", "login: " + PhoneUtils.getUniquePsuedoID()) |
||||||
|
map["code"] = PhoneUtils.getUniquePsuedoID() |
||||||
|
val call = accountService.login(Urls.login, mapToBody(map)) |
||||||
|
call.enqueue(ServiceCallback(LoginEvent::class.java)) |
||||||
|
} |
||||||
|
|
||||||
|
fun getBookArticleDetail(bookId: String?, bookChapters: List<TxtChapter>) { |
||||||
|
val size = bookChapters.size |
||||||
|
//取消上次的任务,防止多次加载 |
||||||
|
if (mChapterSub != null) { |
||||||
|
mChapterSub!!.cancel() |
||||||
|
} |
||||||
|
val chapterInfos = ArrayList<Single<ChapterInfoBean>>(bookChapters.size) |
||||||
|
val titles = ArrayDeque<String>(bookChapters.size) |
||||||
|
|
||||||
|
// 将要下载章节,转换成网络请求。 |
||||||
|
for (i in 0 until size) { |
||||||
|
val bookChapter = bookChapters[i] |
||||||
|
// 网络中获取数据 |
||||||
|
val chapterInfoSingle = getChapterInfo(bookChapter.chapterId) |
||||||
|
chapterInfos.add(chapterInfoSingle) |
||||||
|
titles.add(bookChapter.title) |
||||||
|
} |
||||||
|
|
||||||
|
Single.concat(chapterInfos) |
||||||
|
.subscribeOn(Schedulers.io()) |
||||||
|
.observeOn(AndroidSchedulers.mainThread()) |
||||||
|
.subscribe(object : Subscriber<ChapterInfoBean> { |
||||||
|
internal var title = titles.poll() |
||||||
|
|
||||||
|
override fun onSubscribe(s: Subscription) { |
||||||
|
s.request(Integer.MAX_VALUE.toLong()) |
||||||
|
mChapterSub = s |
||||||
|
} |
||||||
|
|
||||||
|
override fun onNext(chapterInfoBean: ChapterInfoBean) { |
||||||
|
//存储数据 |
||||||
|
BookRepository.getInstance().saveChapterInfo( |
||||||
|
bookId, title, chapterInfoBean.body |
||||||
|
) |
||||||
|
EventManager.instance.postEvent(FinishChapterEvent()) |
||||||
|
//将获取到的数据进行存储 |
||||||
|
title = titles.poll() |
||||||
|
} |
||||||
|
|
||||||
|
override fun onError(t: Throwable) { |
||||||
|
//只有第一个加载失败才会调用errorChapter |
||||||
|
if (bookChapters[0].title == title) { |
||||||
|
EventManager.instance.postEvent(ErrorChapterEvent()) |
||||||
|
} |
||||||
|
LogUtils.e(t) |
||||||
|
} |
||||||
|
|
||||||
|
override fun onComplete() {} |
||||||
|
} |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 注意这里用的是同步请求 |
||||||
|
*/ |
||||||
|
fun getChapterInfo(id: String): Single<ChapterInfoBean> { |
||||||
|
val map = HashMap<String, String>() |
||||||
|
map["article_id"] = id |
||||||
|
return accountService.getBookArticleDetail(getUrlString(Urls.getDetail, map)) |
||||||
|
.map { bean -> bean.article[0] } |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* get方法拼接字符串 |
||||||
|
*/ |
||||||
|
private fun getUrlString(path: String, query: HashMap<String, String>?): String { |
||||||
|
var mypath = path |
||||||
|
if (query != null && query.size > 0) { |
||||||
|
val pathWithQuery = StringBuilder(path) |
||||||
|
if (!path.contains("?")) { |
||||||
|
pathWithQuery.append("?") |
||||||
|
} else { |
||||||
|
pathWithQuery.append("&") |
||||||
|
} |
||||||
|
|
||||||
|
for (stringStringEntry in query.entries) { |
||||||
|
val key = (stringStringEntry as Map.Entry<*, *>).key as String |
||||||
|
val `val` = (stringStringEntry as Map.Entry<*, *>).value as String |
||||||
|
pathWithQuery.append(key) |
||||||
|
pathWithQuery.append("=") |
||||||
|
pathWithQuery.append(`val`) |
||||||
|
pathWithQuery.append("&") |
||||||
|
} |
||||||
|
|
||||||
|
pathWithQuery.deleteCharAt(pathWithQuery.length - 1) |
||||||
|
mypath = pathWithQuery.toString() |
||||||
|
} |
||||||
|
|
||||||
|
return mypath |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 减少请求info类的数量,直接用map替代实体类,返回body |
||||||
|
*/ |
||||||
|
private fun mapToBody(map: HashMap<String, String>): RequestBody { |
||||||
|
return RequestBody.create( |
||||||
|
MediaType.parse("application/json; charset=utf-8"), |
||||||
|
ServiceGenerator.formatResponse(map) |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
companion object { |
||||||
|
|
||||||
|
private var instance: AccountManager? = null |
||||||
|
|
||||||
|
@Synchronized |
||||||
|
fun getInstance(): AccountManager { |
||||||
|
if (instance == null) { |
||||||
|
instance = AccountManager() |
||||||
|
} |
||||||
|
return instance as AccountManager |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,94 +0,0 @@ |
|||||||
package com.novel.read.http.service; |
|
||||||
|
|
||||||
import com.mango.mangolib.http.ErrorResponse; |
|
||||||
import com.novel.read.model.protocol.BookArticleResp; |
|
||||||
import com.novel.read.model.protocol.BookDetailResp; |
|
||||||
import com.novel.read.model.protocol.CategoryTypeResp; |
|
||||||
import com.novel.read.model.protocol.ChapterInfoPackage; |
|
||||||
import com.novel.read.model.protocol.HotSearchResp; |
|
||||||
import com.novel.read.model.protocol.MarkResp; |
|
||||||
import com.novel.read.model.protocol.RankByUpadateResp; |
|
||||||
import com.novel.read.model.protocol.RecommendBookResp; |
|
||||||
import com.novel.read.model.protocol.RecommendListResp; |
|
||||||
import com.novel.read.model.protocol.SearchResp; |
|
||||||
import com.novel.read.model.protocol.UidResp; |
|
||||||
import com.novel.read.model.protocol.VersionResp; |
|
||||||
|
|
||||||
import io.reactivex.Single; |
|
||||||
import okhttp3.RequestBody; |
|
||||||
import retrofit2.Call; |
|
||||||
import retrofit2.http.Body; |
|
||||||
import retrofit2.http.GET; |
|
||||||
import retrofit2.http.POST; |
|
||||||
import retrofit2.http.Url; |
|
||||||
|
|
||||||
/** |
|
||||||
* Created by Administrator on 2017/2/14. |
|
||||||
*/ |
|
||||||
|
|
||||||
public interface AccountService { |
|
||||||
|
|
||||||
//获取推荐书籍
|
|
||||||
@GET |
|
||||||
Call<RecommendBookResp> getRecommendBook(@Url String url); |
|
||||||
|
|
||||||
//获取书籍详情
|
|
||||||
@GET |
|
||||||
Call<BookDetailResp> getBookDetail(@Url String url); |
|
||||||
|
|
||||||
//获取书籍详情
|
|
||||||
@GET |
|
||||||
Single<BookDetailResp> getBookDetails(@Url String url); |
|
||||||
|
|
||||||
//小说类型
|
|
||||||
@GET |
|
||||||
Call<CategoryTypeResp> getCategoryType(@Url String url); |
|
||||||
|
|
||||||
//热搜
|
|
||||||
@GET |
|
||||||
Call<HotSearchResp> getHotSearch(@Url String url); |
|
||||||
|
|
||||||
//书籍章节
|
|
||||||
@GET |
|
||||||
Call<BookArticleResp> getBookArticle(@Url String url); |
|
||||||
|
|
||||||
//搜索
|
|
||||||
@GET |
|
||||||
Call<SearchResp> getSearchList(@Url String url); |
|
||||||
|
|
||||||
//获取推荐
|
|
||||||
@GET |
|
||||||
Call<RecommendListResp> getRecommendList(@Url String url); |
|
||||||
|
|
||||||
//获取最新排行
|
|
||||||
@GET |
|
||||||
Call<RankByUpadateResp> getRankByUpdate(@Url String url); |
|
||||||
|
|
||||||
//获取排行
|
|
||||||
@GET |
|
||||||
Call<RankByUpadateResp> getRankList(@Url String url); |
|
||||||
|
|
||||||
//获取新版本
|
|
||||||
@GET |
|
||||||
Call<VersionResp> checkVersion(@Url String url); |
|
||||||
|
|
||||||
//添加标签
|
|
||||||
@POST |
|
||||||
Call<ErrorResponse> addSign(@Url String url, @Body RequestBody body); |
|
||||||
|
|
||||||
//删除标签
|
|
||||||
@POST |
|
||||||
Call<ErrorResponse> deleteSign(@Url String url, @Body RequestBody body); |
|
||||||
|
|
||||||
//获取标签
|
|
||||||
@GET |
|
||||||
Call<MarkResp> getSignList(@Url String url); |
|
||||||
|
|
||||||
//登录
|
|
||||||
@POST |
|
||||||
Call<UidResp> login(@Url String url, @Body RequestBody body); |
|
||||||
|
|
||||||
//获取书籍章节详情
|
|
||||||
@GET |
|
||||||
Single<ChapterInfoPackage> getBookArticleDetail(@Url String url); |
|
||||||
} |
|
@ -0,0 +1,94 @@ |
|||||||
|
package com.novel.read.http.service |
||||||
|
|
||||||
|
import com.mango.mangolib.http.ErrorResponse |
||||||
|
import com.novel.read.model.protocol.BookArticleResp |
||||||
|
import com.novel.read.model.protocol.BookDetailResp |
||||||
|
import com.novel.read.model.protocol.CategoryTypeResp |
||||||
|
import com.novel.read.model.protocol.ChapterInfoPackage |
||||||
|
import com.novel.read.model.protocol.HotSearchResp |
||||||
|
import com.novel.read.model.protocol.MarkResp |
||||||
|
import com.novel.read.model.protocol.RankByUpadateResp |
||||||
|
import com.novel.read.model.protocol.RecommendBookResp |
||||||
|
import com.novel.read.model.protocol.RecommendListResp |
||||||
|
import com.novel.read.model.protocol.SearchResp |
||||||
|
import com.novel.read.model.protocol.UidResp |
||||||
|
import com.novel.read.model.protocol.VersionResp |
||||||
|
|
||||||
|
import io.reactivex.Single |
||||||
|
import okhttp3.RequestBody |
||||||
|
import retrofit2.Call |
||||||
|
import retrofit2.http.Body |
||||||
|
import retrofit2.http.GET |
||||||
|
import retrofit2.http.POST |
||||||
|
import retrofit2.http.Url |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by Administrator on 2017/2/14. |
||||||
|
*/ |
||||||
|
|
||||||
|
interface AccountService { |
||||||
|
|
||||||
|
//获取推荐书籍 |
||||||
|
@GET |
||||||
|
fun getRecommendBook(@Url url: String): Call<RecommendBookResp> |
||||||
|
|
||||||
|
//获取书籍详情 |
||||||
|
@GET |
||||||
|
fun getBookDetail(@Url url: String): Call<BookDetailResp> |
||||||
|
|
||||||
|
//获取书籍详情 |
||||||
|
@GET |
||||||
|
fun getBookDetails(@Url url: String): Single<BookDetailResp> |
||||||
|
|
||||||
|
//小说类型 |
||||||
|
@GET |
||||||
|
fun getCategoryType(@Url url: String): Call<CategoryTypeResp> |
||||||
|
|
||||||
|
//热搜 |
||||||
|
@GET |
||||||
|
fun getHotSearch(@Url url: String): Call<HotSearchResp> |
||||||
|
|
||||||
|
//书籍章节 |
||||||
|
@GET |
||||||
|
fun getBookArticle(@Url url: String): Call<BookArticleResp> |
||||||
|
|
||||||
|
//搜索 |
||||||
|
@GET |
||||||
|
fun getSearchList(@Url url: String): Call<SearchResp> |
||||||
|
|
||||||
|
//获取推荐 |
||||||
|
@GET |
||||||
|
fun getRecommendList(@Url url: String): Call<RecommendListResp> |
||||||
|
|
||||||
|
//获取最新排行 |
||||||
|
@GET |
||||||
|
fun getRankByUpdate(@Url url: String): Call<RankByUpadateResp> |
||||||
|
|
||||||
|
//获取排行 |
||||||
|
@GET |
||||||
|
fun getRankList(@Url url: String): Call<RankByUpadateResp> |
||||||
|
|
||||||
|
//获取新版本 |
||||||
|
@GET |
||||||
|
fun checkVersion(@Url url: String): Call<VersionResp> |
||||||
|
|
||||||
|
//添加标签 |
||||||
|
@POST |
||||||
|
fun addSign(@Url url: String, @Body body: RequestBody): Call<ErrorResponse> |
||||||
|
|
||||||
|
//删除标签 |
||||||
|
@POST |
||||||
|
fun deleteSign(@Url url: String, @Body body: RequestBody): Call<ErrorResponse> |
||||||
|
|
||||||
|
//获取标签 |
||||||
|
@GET |
||||||
|
fun getSignList(@Url url: String): Call<MarkResp> |
||||||
|
|
||||||
|
//登录 |
||||||
|
@POST |
||||||
|
fun login(@Url url: String, @Body body: RequestBody): Call<UidResp> |
||||||
|
|
||||||
|
//获取书籍章节详情 |
||||||
|
@GET |
||||||
|
fun getBookArticleDetail(@Url url: String): Single<ChapterInfoPackage> |
||||||
|
} |
Loading…
Reference in new issue