working on network

This commit is contained in:
mwang 2019-04-06 23:04:42 +08:00
parent 90af9bbce2
commit bd17a920c0
9 changed files with 200 additions and 97 deletions

View File

@ -33,6 +33,7 @@ import com.novelbook.android.netutils.OnSuccessAndFaultListener;
import com.novelbook.android.netutils.OnSuccessAndFaultSub;
import com.novelbook.android.utils.BookUtil;
import com.novelbook.android.utils.GsonUtil;
import com.novelbook.android.utils.MyImageLoader;
import com.novelbook.android.utils.NovelParseUtil;
import com.novelbook.android.utils.PageFactory;
import com.youth.banner.loader.ImageLoader;
@ -125,8 +126,12 @@ public class BookActivity extends Activity_base {
@Override
protected void setTitle() {
String title = getIntent().getStringExtra("BOOKNAME");
this.setTitle(title);
// String title = getIntent().getStringExtra("BOOKNAME");
// this.setTitle(title);
int bookId = getIntent().getIntExtra("bookId",0);
// this.setTitle("bookName");
}
@Override
@ -146,9 +151,10 @@ public class BookActivity extends Activity_base {
private void setBookInfo() {
}
private MyLoader loader = new MyLoader();
private MyImageLoader loader = new MyImageLoader();
public void setBookDetailInfo( ){
setShelfButtonText();
this.setTitle(mNovel.getName());//why not apply
this.txtAuth.setText(mNovel.getAuthor());
this.txtCategory.setText(mNovel.getNovelType());
this.txtDesc.setText(mNovel.getDescription());
@ -156,7 +162,6 @@ public class BookActivity extends Activity_base {
this.txtDesc2.setText(mNovel.getDescription());
this.txtLatestCate.setText(mNovel.getLastestChapterName());
if(mNovel.getLastUpateTime()>0){
;
this.txtLatestCate.setText( new Date(mNovel.getLastUpateTime()).toString() +"\n"+txtLatestCate.getText());
}
@ -170,12 +175,7 @@ public class BookActivity extends Activity_base {
}
private class MyLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load((String) path).into(imageView);
}
}
void setShelfButtonText(){
String title = mNovel.isOnShelf()?"移除书架":"加入书架";
btnShelf.setText(title);
@ -283,30 +283,7 @@ public class BookActivity extends Activity_base {
void getMuluInfo(){
BookSubscribe.getNovelSites(novelId,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(String result) {
//成功
try {
NovelSites nvs = (NovelSites) gson.fromJson(result,NovelSites.class);
pageFactory.prepareBook(mNovel,nvs,BookActivity.this);
} catch ( Exception e) {
e.printStackTrace();
}
Toast.makeText(BookActivity.this,"getMuluInfo 请求成功 " ,Toast.LENGTH_SHORT).show();
}
@Override
public void onFault(String errorMsg) {
//失败
Toast.makeText(BookActivity.this,"getMuluInfo 请求失败"+errorMsg,Toast.LENGTH_SHORT).show();
}
},BookActivity.this));
}
void getBookInfo(){
@ -322,47 +299,36 @@ public class BookActivity extends Activity_base {
nv = gson.fromJson(result,Novel.class);
// nv = GsonUtil.getNovel(result);
if(!isLocalDbExist){
// nv.saveAsync();
nv.save ();
}else{
List<Novel> nvs = LitePal.where("novelId=?",novelId).find(Novel.class);
for (Novel novel:nvs
) {
Log.d(TAG,String.format("novel id %s before update: lastUpdateTime: %s",novel.getId(),novel.getLastUpateTime()));
}
// nv.setLastUpateTime(new Date().getTime());
nv.updateAll("novelId=?",novelId);
nvs = LitePal.where("novelId=?",novelId).find(Novel.class);
for (Novel novel:nvs
) {
Log.d(TAG,String.format("novel id %s after update: lastUpdateTime: %s",novel.getId(),novel.getLastUpateTime()));
}
}
List<Novel> nvs = LitePal.where("novelId=?",novelId).find(Novel.class);
if(nvs.size()>0) {
mNovel = nvs.get(0);
mNovel = Novel.getNovelBySvrId(novelId);
if(null != mNovel) {
handler.sendEmptyMessage(1);
}
getMuluInfo();
Toast.makeText(BookActivity.this,"Novel 请求成功:"+result,Toast.LENGTH_SHORT).show();
pageFactory.prepareBook(mNovel, BookActivity.this);
// Toast.makeText(BookActivity.this,"Novel 请求成功:"+result,Toast.LENGTH_SHORT).show();
}
@Override
public void onFault(String errorMsg) {
//失败
Toast.makeText(BookActivity.this,"Novel 请求失败:"+errorMsg,Toast.LENGTH_SHORT).show();
mNovel = Novel.getNovelBySvrId(novelId);
if(null != mNovel) {
handler.sendEmptyMessage(1);
pageFactory.prepareBook(mNovel, BookActivity.this);
}
}
},BookActivity.this));

View File

@ -6,6 +6,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.pdf.PdfDocument;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
@ -26,6 +27,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.novelbook.android.Activity_paihangbang;
import com.novelbook.android.Activity_shudan;
import com.novelbook.android.BookActivity;
@ -33,9 +35,14 @@ import com.novelbook.android.R;
import com.novelbook.android.ReadActivity;
import com.novelbook.android.adapter.BookListAdapter;
import com.novelbook.android.db.Novel;
import com.novelbook.android.netsubscribe.BookSubscribe;
import com.novelbook.android.netutils.OnSuccessAndFaultListener;
import com.novelbook.android.netutils.OnSuccessAndFaultSub;
import com.novelbook.android.utils.OnItemClickListener;
import com.novelbook.android.utils.PageFactory;
import org.litepal.LitePal;
import org.w3c.dom.Text;
import java.io.File;
import java.lang.reflect.Field;
@ -57,6 +64,7 @@ public abstract class BasicFragment extends Fragment {
protected abstract void initData();
protected void initListener(){}
protected abstract void initViews();
protected Gson gson = new Gson();
// Main2Activity activity;
FragmentActivity activity;
@ -129,11 +137,39 @@ public abstract class BasicFragment extends Fragment {
void showBookDetail(int id) {
Intent intent = new Intent(activity, BookActivity.class);
intent.putExtra("BOOKNAME",id);
intent.putExtra("bookId",id);
startActivity(intent);
}
void getBookInfo(Novel novel){
String novelId = novel.getNovelId();
long id = novel.getId();
BookSubscribe.getNovel(novel.getNovelId(),new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(String result) {
//成功
Novel nv ;
nv = gson.fromJson(result,Novel.class);
nv.update(id);
nv= LitePal.find(Novel.class,id);
PageFactory.getInstance().prepareBook(nv, getActivity());
}
@Override
public void onFault(String errorMsg) {
//失败
Toast.makeText(getActivity(),"Novel 请求失败:"+errorMsg,Toast.LENGTH_SHORT).show();
PageFactory.getInstance().prepareBook(novel, getActivity()); //打开本地小说内容
}
},getActivity()));
}
void openBook(Novel book, BookListAdapter mAdapter) {
//从新从数据库抓取该书的最新阅读进度
@ -145,14 +181,15 @@ public abstract class BasicFragment extends Fragment {
}
Toast.makeText(activity, book.getName() + "加载", Toast.LENGTH_SHORT).show();
final String path = book.getNovelPath();
if(null ==path) {
List<Novel> bookLists = LitePal.findAll(Novel.class);
if(bookLists.size()>0) {
openBook(bookLists.get(0),null);
}
return;
if(!book.isLocalBook()){
getBookInfo(book);
ReadActivity.openBook(book ,activity);
return;
}
final String path = book.getNovelPath();
File file = new File(path);
if (!file.exists()){
new AlertDialog.Builder(activity)
@ -161,12 +198,17 @@ public abstract class BasicFragment extends Fragment {
.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LitePal.deleteAll(Novel.class, "bookpath = ?", path);
if(mAdapter!=null) {mAdapter.setBookList( LitePal.findAll(Novel.class));}
LitePal.deleteAll(Novel.class, "id = ?", book.getId()+"");
if(mAdapter!=null) {mAdapter.setBookList( LitePal.where("isOnShelf=?","1").find(Novel.class));}
}
}).setCancelable(true).show();
return;
}
ReadActivity.openBook(book ,activity);
}

View File

@ -85,8 +85,8 @@ public class Fragment_Shelf extends BasicFragment {
flag = new boolean[100];
// mDatas = initData(mDatas,'X');
bookLists = LitePal.where("isOnShelf=? or novelId=? ","1","").find(Novel.class);
// bookLists = LitePal.where("isOnShelf=? or novelId=? ","1","").find(Novel.class);
bookLists = LitePal.where("isOnShelf=? ","1" ).find(Novel.class);
mAdapter = new BookListAdapter(activity,bookLists,R.layout.recycle_list_item,new OnItemClickListener()

View File

@ -2,12 +2,14 @@ package com.novelbook.android.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.novelbook.android.db.Novel;
import com.novelbook.android.utils.MyImageLoader;
import com.novelbook.android.utils.OnItemClickListener;
import java.util.ArrayList;
@ -17,7 +19,7 @@ public class BookListAdapter extends RecyclerView.Adapter< MyViewHolder> {
private final int EMPTY_VIEW = 1;
private final int PROGRESS_VIEW = 2;
private final int IMAGE_VIEW = 3;
private MyImageLoader loader = new MyImageLoader();
private Context context;
private List<Novel> mDatas ;
private OnItemClickListener mOnItemClickListener;
@ -84,6 +86,9 @@ public class BookListAdapter extends RecyclerView.Adapter< MyViewHolder> {
if (holder.tvAuthor != null) holder.tvAuthor.setText(mDatas.get(position).getAuthor());
if (holder.tvCate != null) holder.tvCate.setText(mDatas.get(position).getNovelType());
if (holder.tvDesc != null) holder.tvDesc.setText(mDatas.get(position).getDescription());
if (holder.imageView != null && !TextUtils.isEmpty(mDatas.get(position).getCover())) {
loader.displayImage(context,mDatas.get(position).getCover(),holder.imageView);
}
if(holder.tvNum!=null)holder.tvNum.setText("999");
// 如果设置了回调则设置点击事件
if (mOnItemClickListener != null) {

View File

@ -1,10 +1,15 @@
package com.novelbook.android.db;
import android.text.TextUtils;
import org.litepal.LitePal;
import org.litepal.annotation.Column;
import org.litepal.crud.LitePalSupport;
import org.w3c.dom.Text;
import java.io.Serializable;
import java.util.List;
public class Novel extends LitePalSupport implements Serializable{
@ -173,4 +178,26 @@ public class Novel extends LitePalSupport implements Serializable{
public void setFinished(boolean finished) {
isFinished = finished;
}
public boolean isLocalBook(){
return TextUtils.isEmpty(novelId);
}
public static Novel getNovelBySvrId(String novelId){
List<Novel> nvs = LitePal.where("novelId=?",novelId).limit(1).find(Novel.class);
if(nvs.size()>0){
return nvs.get(0);
}
return null;
}
public static List<Novel> getNovelsOnShelf(){
return LitePal.where("isOnShelf=?","1").find(Novel.class);
}
public static List<Novel> getLocalNovels(){
return LitePal.where("novelPath !=? ","").find(Novel.class);
}
}

View File

@ -289,7 +289,7 @@ public class DirectoryFragment extends Fragment implements View.OnClickListener
@Override
public void onResume() {
super.onResume();
bookLists = LitePal.where("isOnShelf=?","1").find( Novel.class);
bookLists = Novel.getLocalNovels(); // LitePal.where("isOnShelf=?","1").find( Novel.class);
listAdapter.notifyDataSetChanged();
}
@ -391,7 +391,7 @@ public class DirectoryFragment extends Fragment implements View.OnClickListener
case SUCCESS:
msg = "导入书本成功";
checkItems.clear();
bookLists = LitePal.where("isOnShelf=?","1").find( Novel.class);//LitePal.findAll(Novel.class);
bookLists = Novel.getLocalNovels();//LitePal.where("isOnShelf=?","1").find( Novel.class);//LitePal.findAll(Novel.class);
listAdapter.notifyDataSetChanged();
changgeCheckBookNum();
break;

View File

@ -13,6 +13,7 @@ import android.util.Log;
import android.widget.Toast;
import com.google.gson.Gson;
import com.novelbook.android.BookActivity;
import com.novelbook.android.MyApp;
import com.novelbook.android.bean.Cache;
import com.novelbook.android.bean.NovelSites;
@ -121,15 +122,45 @@ public class BookUtil {
break;
}
}
getSiteRule();
}
private void setSiteInfo() {
File file = new File(getChapterPath() +mSite.getDomain());
if(!file.exists()){
file.mkdir();
}
getSiteRule();
mNovel.setDomain(mSite.getDomain());
mNovel.setMuluUrl(mSite.getMuluUrl());
mNovel.save();
}
public void getTargetSites(){
BookSubscribe.getNovelSites(mNovel.getNovelId(),new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(String result) {
//成功
try {
NovelSites nvs = (NovelSites) gson.fromJson(result,NovelSites.class);
//pageFactory.prepareBook(mNovel,nvs, BookActivity.this);
setNovelSites(nvs);
} catch ( Exception e) {
e.printStackTrace();
}
Toast.makeText(mContext,"getMuluInfo 请求成功 " ,Toast.LENGTH_SHORT).show();
}
@Override
public void onFault(String errorMsg) {
//失败
Toast.makeText(mContext,"getMuluInfo 请求失败"+errorMsg,Toast.LENGTH_SHORT).show();
}
},mContext));
}
private void getSiteRule() {
mSiteRule = null;
BookSubscribe.getSiteRule(mSite.getDomain(),new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@ -148,7 +179,7 @@ public class BookUtil {
sr.save();
}
mSiteRule =sr;
setSiteInfo();
readChaptersAsync();
@ -157,10 +188,17 @@ public class BookUtil {
@Override
public void onFault(String errorMsg) {
//失败
Log.d(TAG,"error on get sitRule: "+errorMsg);
}
},mContext));
/*
if(mSiteRule==null && mSite!=null) {
List<SiteRule> srs = LitePal.where("domain=?", mSite.getDomain()).find(SiteRule.class);
if (srs.size() > 0) {
mSiteRule = srs.get(0);
}
}*/
}
@ -222,7 +260,7 @@ public class BookUtil {
boolean isLocalImport = TextUtils.isEmpty( novel.getNovelId());
boolean isOnShelf = isLocalImport || novel.isOnShelf();
boolean isLoadChaptsFromRemote = !isLocalImport ;// && !novel.isFinished() ; //是否从目标网站下载目录
showProgressDialog();
if(isLocalImport) {
mChapters = LitePal.where("novelId=?", mNovel.getId() + "").find(Chapter.class);
@ -243,14 +281,18 @@ public class BookUtil {
}
}else{ //读取目录列表
MuluStatus m = mMuluStatus;
Log.d(TAG,String.format("mulu on Site %s download status %s",mSite.getDomain(),mMuluStatus));
// Log.d(TAG,String.format("mulu on Site %s download status %s",mSite.getDomain(),mMuluStatus));
while(mMuluStatus == MuluStatus.isDownloading){
Thread.sleep(50);
Log.d(TAG,String.format("waiting for mulu downloading ,mMuluStatus %s" ,mMuluStatus));
if(mMuluStatus == MuluStatus.failed){
throw new RuntimeException("读取资源失败,请检查网络");
}
}
}
dismissProgressDialog();
}
// String getMuluUrl() {
@ -298,7 +340,7 @@ public class BookUtil {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: " + e.getMessage());
if( mNovelSites.getSites().length ==1){
if( mNovelSites.getSites().length ==1){ //仅有一个rule,且失败了
mMuluStatus = MuluStatus.failed;
return;
}
@ -317,6 +359,10 @@ public class BookUtil {
@Override
public void onResponse(Call call, Response response){
ResponseBody body = response.body();
if(response.code()!=200){
mMuluStatus = MuluStatus.failed;
return;
}
if (body != null) {
try {
@ -704,9 +750,9 @@ public class BookUtil {
return cachedPath + mNovel.getName() + index ;
}
protected String fileChapterName(int chaptId ) {
if(mSite!=null){
if(TextUtils.isEmpty(mNovel.getDomain())){
return getChapterPath() +mSite.getDomain()+"/"+ chaptId ;
return getChapterPath() +mNovel.getDomain()+"/"+ chaptId ;
}
return getChapterPath() + chaptId ;
}
@ -789,22 +835,29 @@ public class BookUtil {
if(!file.exists()) {
if(mChapters ==null || mChapters.size() ==0){
String error = "网络错误";
return error.toCharArray();
}
Log.d(TAG,String.format("loadChaptContent----start %s" ,new Date().toString() ));
loadChaptContent(index);
showProgressDialog();//why not show
// showProgressDialog();//why not show
int slepttime =0;
while(!isDownloadChapt){
Thread.sleep(50);
slepttime+=50;
Log.d(TAG,String.format("loadChaptContent slept %s for downloading ",slepttime ) );
}
dismissProgressDialog();
Log.d(TAG,String.format("loadChaptContent slept %s for downloading %s ",slepttime, mChapters.get(index -1).getChapterName() ));
// dismissProgressDialog();
// Log.d(TAG,String.format("loadChaptContent slept %s for downloading %s ",slepttime, mChapters.get(index -1).getChapterName() ));
}
if(!file.exists()) {
String error = "网络错误";
String error = "网络错误";
return error.toCharArray();
}
if(mChapters.size() > index ) {
@ -856,9 +909,7 @@ private void loadChaptContent(int index) throws JSONException, InterruptedExcept
2. 查询主服务器若有地址更新则更新本地信息并重复1若没有更新地址则地址无效返回章节内容正待手打
*/
//
if(mChapters ==null || mChapters.size() ==0){
return;
}
Chapter chapter = mChapters.get(index -1);
String url = chapter.getChapterUrl();
if( TextUtils.isEmpty( url)){

View File

@ -0,0 +1,14 @@
package com.novelbook.android.utils;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MyImageLoader extends com.youth.banner.loader.ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load((String) path).into(imageView);
}
}

View File

@ -508,17 +508,14 @@ public class PageFactory {
currentPage = cancelPage;
}
public void prepareBook(Novel book,NovelSites nvs,Context context){
public void prepareBook(Novel book, Context context){
if(mBook!=null &&mBook.getNovelId() !=book.getNovelId()){ //取消未上本书完成的web请求待验证效果
NetUtil.cancelRequest(mBook.getNovelId());
}
mBookUtil = new BookUtil();
this.mBookUtil.setContext(context);
this.mBookUtil.setNovel(book);
this.mBookUtil.setNovelSites(nvs);
this.mBookUtil.getTargetSites();
}
/**
@ -526,10 +523,11 @@ public class PageFactory {
* @throws IOException
*/
public void openBook(Novel book ,Context context) throws IOException {
if(null ==mBookUtil || !TextUtils.isEmpty(book.getNovelPath()) ){ //离线书籍重新初始化加载mBookUtil
mBookUtil = new BookUtil();
mBookUtil.setContext(context);
}
if(book.isLocalBook() ){ //离线书籍重新初始化加载mBookUtil
mBookUtil = new BookUtil();
mBookUtil.setContext(context);
}
//清空数据
currentChapter = 0;
// m_mbBufLen = 0;