Search in sources :

Example 46 with Connection

use of org.jsoup.Connection in project Tusky by Vavassor.

the class ParserUtils method parsePageHeaderInfo.

// parse the HTML page
private HeaderInfo parsePageHeaderInfo(String urlStr) throws Exception {
    Connection con = Jsoup.connect(urlStr);
    HeaderInfo headerInfo = new HeaderInfo();
    con.userAgent(HttpConnection.DEFAULT_UA);
    Document doc = con.get();
    // get info
    String text;
    Elements metaOgTitle = doc.select("meta[property=og:title]");
    if (metaOgTitle != null) {
        text = metaOgTitle.attr("content");
    } else {
        text = doc.title();
    }
    String imageUrl = null;
    Elements metaOgImage = doc.select("meta[property=og:image]");
    if (metaOgImage != null) {
        imageUrl = metaOgImage.attr("content");
    }
    // set info
    headerInfo.baseUrl = urlStr;
    if (!TextUtils.isEmpty(text)) {
        headerInfo.title = QUOTE + text.toUpperCase() + QUOTE;
    }
    if (!TextUtils.isEmpty(imageUrl)) {
        headerInfo.image = (imageUrl);
    }
    return headerInfo;
}
Also used : Connection(org.jsoup.Connection) HttpConnection(org.jsoup.helper.HttpConnection) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 47 with Connection

use of org.jsoup.Connection in project SmartCampus by Vegen.

the class MoreNewsPresenter method getNewsList.

@Override
public void getNewsList(int page, String url, int newsType, String view_state, String view_state_generator, String event_validation) {
    Disposable disposable = Observable.create((ObservableOnSubscribe<Document>) e -> {
        Connection connection = Jsoup.connect(url);
        connection.data("__EVENTARGUMENT", "");
        connection.data("__EVENTTARGET", "PageNavigator1$LnkBtnNext");
        connection.data("__EVENTVALIDATION", event_validation);
        connection.data("__VIEWSTATE", view_state);
        connection.data("__VIEWSTATEGENERATOR", view_state_generator);
        connection.data("Date", "30");
        connection.data("tags", "");
        Document document = connection.post();
        e.onNext(document);
        e.onComplete();
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(document -> {
        Element body = document.body();
        // 下一页的参数
        String __VIEWSTATE = body.select("#__VIEWSTATE").first().attr("value").trim();
        String __VIEWSTATEGENERATOR = body.select("#__VIEWSTATEGENERATOR").first().attr("value").trim();
        String __EVENTVALIDATION = body.select("#__EVENTVALIDATION").first().attr("value").trim();
        Map<String, String> newsForm = new HashMap<>();
        newsForm.put("__VIEWSTATE", __VIEWSTATE);
        newsForm.put("__VIEWSTATEGENERATOR", __VIEWSTATEGENERATOR);
        newsForm.put("__EVENTVALIDATION", __EVENTVALIDATION);
        // 本页的数据
        Element photoList = body.getElementById("ListNews").getElementById("leftbox").getElementById("photolist");
        List<News> newsList = getTagNews(photoList, newsType);
        if (mView != null) {
            mView.nextNewsListForm(newsForm);
            mView.showNewsList(newsList);
            mView.hideLoading(false);
        }
    }, throwable -> {
        if (mView != null) {
            mView.showMessage(HttpError.getErrorMessage(throwable));
            mView.hideLoading(true);
            mView.loadMoreFail();
        }
    });
    mHttpLinkers.add(new DisposableHolder(disposable));
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) News(com.itculturalfestival.smartcampus.entity.News) DisposableHolder(com.vegen.smartcampus.baseframework.network.DisposableHolder) Connection(org.jsoup.Connection) Document(org.jsoup.nodes.Document)

Aggregations

Connection (org.jsoup.Connection)47 Test (org.junit.Test)42 Document (org.jsoup.nodes.Document)26 ParseTest (org.jsoup.integration.ParseTest)13 IOException (java.io.IOException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Element (org.jsoup.nodes.Element)2 Elements (org.jsoup.select.Elements)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 News (com.itculturalfestival.smartcampus.entity.News)1 DisposableHolder (com.vegen.smartcampus.baseframework.network.DisposableHolder)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Disposable (io.reactivex.disposables.Disposable)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1