Android MVVM框架搭建(六)腾讯X5WebView + DrawerLayout + NavigationView

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

</androidx.constraintlayout.widget.ConstraintLayout>

然后在WebActivity中增加如下代码,用于配置WebView。

private final WebViewClient client = new WebViewClient() {

/**

  • 防止加载网页时调起系统浏览器

*/

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);

return true;

}

@Override

public void onReceivedHttpAuthRequest(WebView webview,

com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost, String host,

String realm) {

boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();

}

@Override

public void onPageFinished(WebView webView, String s) {

super.onPageFinished(webView, s);

}

@Override

public void onReceivedError(WebView webView, int i, String s, String s1) {

System.out.println(“***********onReceivedError ************”);

super.onReceivedError(webView, i, s, s1);

}

@Override

public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {

System.out.println(“***********onReceivedHttpError ************”);

super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);

}

};

当前的页面是需要网络请求的,因此就会有相应的ViewModel和Repository,因为聚合给的新闻数据里面有一个uniquekey,用于查询新闻的详情信息,然后再去返回的详情信息里面找到url通过WebView去加载。当然并不是每一条新闻都能够去显示的,有一些新闻是没有详情信息的,这在我们点击新闻的时候就要做处理。

这是我们下面要做的事情,现在对于X5WebView还需要进行一个初始化,这样做是方便使用的。在BaseApplication中增加如下代码:

private void initX5WebView() {

HashMap map = new HashMap(2);

map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);

map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);

QbSdk.initTbsSettings(map);

//搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。

QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

@Override

public void onViewInitFinished(boolean arg0) {

//x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。

Log.d(“app”, " onViewInitFinished is " + arg0);

}

@Override

public void onCoreInitFinished() {

}

};

//x5内核初始化接口

QbSdk.initX5Environment(getApplicationContext(), cb);

}

然后在onCreate中调用它。

在这里插入图片描述

下面关于WebView的使用就只有一步了,那就是加载url,现在还没有的,去获取它。

三、获取新闻详情


在聚合API中获取新闻详情是另一个接口,在写这个接口之前,先写一个返回的新闻详情数据。

① 新闻详情数据

在model包下新增一个NewsDetailResponse类,里面的代码如下:

public class NewsDetailResponse {

private String reason;

private ResultBean result;

private Integer error_code;

public String getReason() {

return reason;

}

public void setReason(String reason) {

this.reason = reason;

}

public ResultBean getResult() {

return result;

}

public void setResult(ResultBean result) {

this.result = result;

}

public Integer getError_code() {

return error_code;

}

public void setError_code(Integer error_code) {

this.error_code = error_code;

}

public static class ResultBean {

private String uniquekey;

private DetailBean detail;

private String content;

public String getUniquekey() {

return uniquekey;

}

public void setUniquekey(String uniquekey) {

this.uniquekey = uniquekey;

}

public DetailBean getDetail() {

return detail;

}

public void setDetail(DetailBean detail) {

this.detail = detail;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public static class DetailBean {

private String title;

private String date;

private String category;

private String author_name;

private String url;

private String thumbnail_pic_s;

private String thumbnail_pic_s02;

private String thumbnail_pic_s03;

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getDate() {

return date;

}

public void setDate(String date) {

this.date = date;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public String getAuthor_name() {

return author_name;

}

public void setAuthor_name(String author_name) {

this.author_name = author_name;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getThumbnail_pic_s() {

return thumbnail_pic_s;

}

public void setThumbnail_pic_s(String thumbnail_pic_s) {

this.thumbnail_pic_s = thumbnail_pic_s;

}

public String getThumbnail_pic_s02() {

return thumbnail_pic_s02;

}

public void setThumbnail_pic_s02(String thumbnail_pic_s02) {

this.thumbnail_pic_s02 = thumbnail_pic_s02;

}

public String getThumbnail_pic_s03() {

return thumbnail_pic_s03;

}

public void setThumbnail_pic_s03(String thumbnail_pic_s03) {

this.thumbnail_pic_s03 = thumbnail_pic_s03;

}

}

}

}

② 新闻详情数据API

/**

  • 聚合新闻数据详情

*/

@GET(“/toutiao/content?key=99d3951ed32af2930afd9b38293a08a2”)

Observable newsDetail(@Query(“uniquekey”) String uniquekey);

这个接口用于请求新闻详情数据,返回值将会解析成NewsDetailResponse。

③ WebRepository

数据有了,API接口有了,下面就是去调用的地方了ÿ

  • 29
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值