try read from web

This commit is contained in:
mwang 2019-04-03 23:29:58 +08:00
parent 355b7adf63
commit 9fb63f10aa
10 changed files with 167 additions and 22 deletions

View File

@ -10,9 +10,11 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.gson.Gson;
import com.novelbook.android.adapter.BookListAdapter;
import com.novelbook.android.db.Chapter;
import com.novelbook.android.db.Novel;
import com.novelbook.android.netsubscribe.BookSubscribe;
import com.novelbook.android.netsubscribe.MovieSubscribe;
import com.novelbook.android.netutils.HttpMethods;
import com.novelbook.android.netutils.OnSuccessAndFaultListener;
@ -26,7 +28,9 @@ import org.litepal.LitePal;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
@ -39,12 +43,15 @@ import okhttp3.ResponseBody;
public class BookActivity extends Activity_base {
String novelId="f2619820112625133c14dcb170f5e092";
String muluUrl="https://www.qu.la/book/390/";
private Novel mNovel;
static String TAG = BookActivity.class.getSimpleName();
BookListAdapter mAdapter;
// private BookListAdapter mAdapter;
private List<Novel> mData;;
private List<Chapter> chapters = new ArrayList<>();
private ArrayList<Chapter> mChapters = new ArrayList<>();
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.rvBooklist)
@ -85,6 +92,9 @@ public class BookActivity extends Activity_base {
@Override
protected void initData() {
getBookInfo();
// getMuluInfo();
mData =getFakeData(5);
mAdapter = getBookListAdapter(mData);
}
@ -95,6 +105,7 @@ public class BookActivity extends Activity_base {
switch (view.getId()) {
case R.id.btnRead:
testBook();
// openBook(new Novel() );
break;
case R.id.btnCacheBook:
@ -147,13 +158,127 @@ public class BookActivity extends Activity_base {
}).setCancelable(true).show();
return;
}
ReadActivity.openBook(book ,chapters,this);
ReadActivity.openBook(book ,mChapters,this);
}
void getMuluInfo(){
BookSubscribe.getNovelMulu(novelId,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(String result) {
//成功
try {
JSONObject jsonObject = new JSONObject(result);
muluUrl= jsonObject.getString("muluUrl");
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(BookActivity.this,"muluUrl 请求成功:" + muluUrl,Toast.LENGTH_SHORT).show();
}
@Override
public void onFault(String errorMsg) {
//失败
Toast.makeText(BookActivity.this,"Novel 请求失败:"+errorMsg,Toast.LENGTH_SHORT).show();
}
},BookActivity.this));
}
void getBookInfo(){
boolean isLocalDbExist = LitePal.isExist(Novel.class,"novelId=?", novelId);
BookSubscribe.getNovel(novelId,new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(String result) {
//成功
Gson gson = new Gson();
// result ={"id":"f2619820112625133c14dcb170f5e092","novelType":"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>","novelType2":"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>½","name":"<EFBFBD><EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD>","author":"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","cover":"http:\/\/qidian.qpic.cn\/qdbimg\/349573\/1209977\/180","description":"","lastestChapterName":"<EFBFBD>ڰ<EFBFBD><EFBFBD><EFBFBD> <20><>ʼ","lastUpateTime":""}
// Novel nv = gson.fromJson(result,Novel.class); //to change id to noveId
Novel nv = new Novel();
try {
JSONObject jsonObject = new JSONObject(result);
nv.setNovelId(jsonObject.getString("id"));
// nv.setLastUpateTime(jsonObject.getLong("lastUpateTime"));
nv.setAuthor(jsonObject.getString("author"));
nv.setNovelName(jsonObject.getString("name"));
nv.setCover(jsonObject.getString("cover"));
nv.setNovelType(jsonObject.getString("novelType"));
nv.setNovelType2(jsonObject.getString("novelType2"));
nv.setLastestChapterName(jsonObject.getString("lastestChapterName"));
} catch (JSONException e) {
e.printStackTrace();
}
//to load image
//to load desc
//...
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);
}
// getMuluInfo();
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();
}
},BookActivity.this));
}
void testBook(){
String url = "https://www.qu.la/book/161/";//"https://www.qu.la/book/746/";
//to get mulu list
String url = muluUrl;// "https://www.qu.la/book/161/";//"https://www.qu.la/book/746/";
Request request = new Request.Builder()
.url(url)
// .header("User-Agent", "OkHttp Example")
@ -173,6 +298,7 @@ public class BookActivity extends Activity_base {
String bodyStr = body.string();
Log.d(TAG, "onResponse: " +bodyStr);
onResponseProcess(bodyStr,url);
ReadActivity.openBook(mNovel ,mChapters,BookActivity.this);
} catch (IOException e) {
e.printStackTrace();
}finally {
@ -215,15 +341,12 @@ void onResponseProcess( String content ,String url){
Log.d(TAG, String.format("%s-->%s", chapters2[i], chapters2[i + 1]));
Chapter chapter = new Chapter();
chapter.setChapterName(chapters2[i + 1]);
chapter.setChapterPath(chapters2[i ]);
chapters.add(chapter);
chapter.setChapterUrl(chapters2[i ]);
mChapters.add(chapter);
}
/*
siteJson.put("chapterContentRegex", "<div id=\"content\">([\\s\\S]+?)</div>");
siteJson.put("chapterContentDumpRegex", "<script>chaptererror();</script>");
@ -236,9 +359,10 @@ void onResponseProcess( String content ,String url){
Response response = HttpMethods.getOkClient().newCall(request).execute();
// Log.d(TAG,String.format("%s-->%s\n%s" , chapters2[i], chapters2[i+1] , NovelParseUtil.getChapterContent(response.body().string(), siteJson)));
}
*/
}
} catch (JSONException | IOException e) {
} catch (JSONException e) {
// } catch (JSONException | IOException e) {
e.printStackTrace();
} finally {
// result.close();

View File

@ -167,7 +167,7 @@ public abstract class BasicFragment extends Fragment {
}).setCancelable(true).show();
return;
}
ReadActivity.openBook(book ,activity);
// ReadActivity.openBook(book ,activity);
}
void showShudanDetail(int shuandanId){

View File

@ -527,14 +527,14 @@ public class ReadActivity extends Activity_base implements SpeechSynthesizerLis
}
public static boolean openBook(final Novel book,List<Chapter> chapters ,Activity context) {
public static boolean openBook(final Novel book,final ArrayList<Chapter> chapters ,Activity context) {
if (book == null){
throw new NullPointerException("Novel can not be null");
}
Intent intent = new Intent(context, ReadActivity.class);
intent.putExtra(EXTRA_BOOK, book);
// intent.putExtra(EXTRA_CHAPTERS, chapters);
intent.putExtra(EXTRA_CHAPTERS, chapters);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);

View File

@ -3,8 +3,10 @@ package com.novelbook.android.db;
import org.litepal.crud.LitePalSupport;
import java.io.Serializable;
public class Chapter extends LitePalSupport {
public class Chapter extends LitePalSupport implements Serializable {
private int id;
private int novelId;
private String novelPath;

View File

@ -649,7 +649,7 @@ public class DirectoryFragment extends Fragment implements View.OnClickListener
if (!isSave){
bookList.save();
}
ReadActivity.openBook(bookList,getActivity());
ReadActivity.openBook(bookList,null,getActivity());
}
}).show();
}

View File

@ -9,6 +9,7 @@ import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.QueryMap;
import retrofit2.http.Streaming;
import retrofit2.http.Url;
@ -34,6 +35,12 @@ public interface HttpApi {
Call<ResponseBody> downloadFile(@Url String fileUrl);
@GET("n/{id}.json")
Observable<ResponseBody> getNovel(@Path("id") String novelId);
@GET("n/{id}.mulu-urls.json")
Observable<ResponseBody> getNovelMulu(@Path("id") String novelId);
@GET("s/{siteName}.json")
Observable<ResponseBody> getNovelRegex(@Path("siteName") String siteName);
}

View File

@ -6,5 +6,5 @@ package com.novelbook.android.netapi;
public class URLConstant {
//存放全部的URL可分为开发测试正式
public static String BASE_URL = "https://api.douban.com/v2/movie/";
public static String BASE_URL = "http://xiaoshuofenxiang.com/api/";//https://api.douban.com/v2/movie/";
}

View File

@ -17,4 +17,14 @@ public class BookSubscribe {
Observable<ResponseBody> observable = HttpMethods.getInstance().getHttpApi().getDataForMap(map);
HttpMethods.getInstance().toSubscribe(observable, subscriber);
}
public static void getNovel(String novelId,DisposableObserver<ResponseBody> subscriber){
Observable<ResponseBody> observable = HttpMethods.getInstance().getHttpApi().getNovel(novelId);
HttpMethods.getInstance().toSubscribe(observable, subscriber);
}
public static void getNovelMulu(String novelId,DisposableObserver<ResponseBody> subscriber){
Observable<ResponseBody> observable = HttpMethods.getInstance().getHttpApi().getNovelMulu(novelId);
HttpMethods.getInstance().toSubscribe(observable, subscriber);
}
}

View File

@ -165,11 +165,11 @@ public class OnSuccessAndFaultSub extends DisposableObserver<ResponseBody>
final String result =body.string();
Log.e("body", result);
JSONObject jsonObject = new JSONObject(result);
int resultCode = jsonObject.getInt("ErrorCode");
if (resultCode == 1) {
mOnSuccessAndFaultListener.onSuccess(result);
int resultCode = jsonObject.getInt("code");
if (resultCode == 0) {
mOnSuccessAndFaultListener.onSuccess(jsonObject.getString("data"));
} else {
String errorMsg = jsonObject.getString("ErrorMessage");
String errorMsg = jsonObject.getString("message");
mOnSuccessAndFaultListener.onFault(errorMsg);
Log.e("OnSuccessAndFaultSub", "errorMsg: " + errorMsg);
}

View File

@ -535,7 +535,9 @@ public class PageFactory {
PageFactory.mStatus = PageFactory.Status.FINISH;
// m_mbBufLen = mBookUtil.getBookLen();
mBookUtil.setChapterNo((int)chapter);
mBookUtil.setChapters(mChapters);
if(mChapters!=null) {
mBookUtil.setChapters(mChapters);
}
currentChaptPages = loadCurrentChapt((int)chapter);
currentPage = getPageForBegin(begin) ;// currentChaptPages.get(0);
// currentPage = getPageForBegin(begin);