×

注意!页面内容来自https://blog.csdn.net/sxs120730/article/details/79076135,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页

将web网站转为App

本文介绍了如何将现有的Web网站转换成一款可以在手机上使用的应用程序,通过封装技术,实现Web内容在移动设备上的无缝浏览体验。

首先添加依赖:
   testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.github.florent37:retrooup:1.0.4'
    compile 'com.github.florent37:rxoup:1.0.4'
    compile 'org.oup:oup:1.10.2'
    compile 'io.reactivex.rxjava2:rxjava:2.1.3'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
   compile 'com.just.agentweb:agentweb:2.0.0'
    testCompile 'junit:junit:4.12'
    annotationProcessor 'com.github.florent37:retrooup-compiler:1.0.4'
 

封装地址:

public class API {
    public static final String BLOG_BASE_URL = "http://www.ruanyifeng.com/blog/archives.html";
}
封装网络请求的类:

package com.shenxuesong.franklog.net;

import com.github.florent37.retrooup.RetroJsoup;

import org.oup.Jsoup;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

public class JsoupHelper {
    private static OkHttpClient mOkHttpClient;

    static {
        initOkHttpClient();
    }

    /**
     * 初始化OKHttpClient,设置缓存,设置超时时间,设置打印日志,设置UA拦截器
     */
    private static void initOkHttpClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        if (mOkHttpClient == null) {
            synchronized (Jsoup.class) {
                if (mOkHttpClient == null) {
                    mOkHttpClient = new OkHttpClient.Builder()
                            .addInterceptor(interceptor)
                            .retryOnConnectionFailure(true)
                            .connectTimeout(30TimeUnit.SECONDS)
                            .writeTimeout(20TimeUnit.SECONDS)
                            .readTimeout(20TimeUnit.SECONDS)
                            .build();
                }
            }
        }
    }

    private static <T> T createApi(Class<T> clazzString webUrl) {
        return new RetroJsoup.Builder()
                .url(webUrl)
                .client(new OkHttpClient())
                .build()
                .create(clazz);
    }

    public static ServiceApi getBlogFeed() {
        return createApi(ServiceApi.classAPI.BLOG_BASE_URL);
    }
}
打开网页查看网页源码!封装bean类

public class ArticleTitle {
    @JsoupText("li")
    public String title;

    @JsoupHref("a")
    public String url;

}
public interface ServiceApi {
   @Select("div#beta-inner li")
   Observable<ArticleTitle> getContent();
    @Select("div.module-categories li")
    Observable<ArticleTitle> getTitle();
}
 Observable<ArticleTitle> observable = JsoupHelper.getBlogFeed().getTitle();
       observable.toList()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<ArticleTitle>>() {
                    @Override
                    public void accept(List<ArticleTitle> articleTitles) throws Exception {
                        successAndFailure.getSuccess(articleTitles);
                    }
                });






确定要放弃本次机会?
福利倒计时
: :

立减 ¥

普通VIP年卡可用
立即使用
2 条评论 您还未登录,请先 登录 后发表或查看评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值