Android源 62,63,64

62.
public class MainActivity extends AppCompatActivity { 
 
    private List<String> strIDList; 
    private List<Document> docsList; 
    private List<KugouMusicInfo> kugouMusicInfosList; 
    private TextView textView; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
 
        visitKugou(); 
        textView = (TextView) findViewById(R.id.text1); 
 
    } 
63.
public void visitKugou(){ 
        //创建线程 
        new Thread(){ 
            @Override 
            public void run() { 
                super.run(); 
                //定义path 
                String path = "http://m.kugou.com"; 
                try { 
              
                    Document doc = Jsoup.connect(path).userAgent("Mozilla/5.0 (Linux;" + 
                            " Android 6.0;" + 
                            " Nexus 5 Build/MRA58N) AppleWebKit/537.36" + 
                            " (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36"). 
                            timeout(5000). 
                            get(); 
                    //设置选择器,查找li标签中的id属性 
                    Elements liID = doc.select("li[id]"); 
                    //测试输出 
                    System.out.println(String.format("li:%d", liID.size())); 
                    //使用list保存提取出来的strID 
                    strIDList = new ArrayList<>(); 
                    for (Element li : liID){ 
                        if (li.tagName().equals("li")){ 
                            System.out.println("<li>" + String.format("%s:%s", 
                                    li.tagName(), 
                                    li.attr("id"))); 
                            String strID = li.attr("id"); 
                            int begin = strID.indexOf("_") + 1; 
                            strID = strID.substring(begin, 38); 
                            System.out.println(strID); 
                            strIDList.add(strID); 
                        } 
                    } 
64.
public class MainActivity extends AppCompatActivity { 
 
    private TextView textView; 
    private List<String> content = new ArrayList<>(); 
 
    //http://m.kugou.com/ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        textView = (TextView) findViewById(R.id.tv_show); 
        textView.setText("抓取中..."); 
        runSubThread(); 
    } 
    public void runSubThread(){ 
        new Thread(){ 
            @Override 
            public void run() { 
                super.run(); 
                String path = "http://m.kugou.com"; 
                try { 
                    Document doc = Jsoup.connect(path).userAgent("Mozilla/5.0 (Linux;" + 
                            " Android 6.0;" + 
                            " Nexus 5 Build/MRA58N) AppleWebKit/537.36" + 
                            " (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36"). 
                            timeout(5000). 
                            get(); 
                    Elements links = doc.select("a[href]"); 
                    Elements media = doc.select("img[src]"); 
                    Elements imports = doc.select("link[href]"); 
                    System.out.println(String.format("Media:(%d)", media.size())); 
                    for (Element src : media) 
                    { 
                        if (src.tagName().equals("img")){ 
                            System.out.println("<img>" + String.format("%s:%s", 
                                    src.tagName(), 
                                    src.attr("abs:src"))); 
                            content.add("<img>" + String.format("%s:%s", 
                                    src.tagName(), 
                                    src.attr("abs:src"))); 
                        } 
                    } 
                    runOnUiThread(new Runnable() { 
                        @Override 
                        public void run() { 
                            for (int i = 0; i < content.size(); i++) 
                                textView.setText(content.get(i)); 
                        } 
                    }); 
                } 
                catch (Exception e) { 
                    e.printStackTrace(); 
                } 
 
            } 
        }.start(); 
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值