app第二阶段冲刺第十天—— jsoup 2

 

今天写的是 jsoup  获取网页 html,抓取到的文章数据封装

 

实现代码如下:

package com.example.crawler.Tools;

import android.util.Log;
import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

//OkHttpUtils 获取网页 html
public class OkHttpUtils {

    final static String TAG = "OkHttpUtils";

    public static String OkGetArt(String url) {
        String html = null;
        OkHttpClient client = new OkHttpClient();//OkHttpClient  HTTP客户端
        Request request = new Request.Builder()
                .url(url)
                .build();
        try  {
            Response response = client.newCall(request).execute();
            //return
            html = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //Log.i(TAG, "OkGetArt: html "+html);
        return html;

    }
}

 

package com.example.crawler.Tools;
/***
 * 抓取到的文章数据封装
 */
//Article 为数据模型类,抓取那些数据类型
public class Article {

    private String title;
    private String author;
    private String imgUrl;
    private String context;
    private String articleUrl;


    //有几个属性还没用到,所以构造方法先用上这四个有爬取到数据的
    public Article(String title, String author, String imgUrl, String context, String articleUrl) {
        this.title = title;
        this.author = author;
        this.imgUrl = imgUrl;
        this.context = context;
        this.articleUrl = articleUrl;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getArticleUrl() {
        return articleUrl;
    }

    public void setArticleUrl(String articleUrl) {
        this.articleUrl = articleUrl;
    }



    @Override
    public String toString() {
        return "Article{" +
                "title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", imgUrl='" + imgUrl + '\'' +
                ", context='" + context + '\'' +
                ", articleUrl='" + articleUrl + '\'' +
                '}';
    }
}

 

 

明天要写的是关于适配器相关的内容

 

posted @ 2022-05-12 22:37  kuaiquxie  阅读(31)  评论(0)    收藏  举报