码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发本地及网络Mp3音乐播放器(十一)使用Jsoup组件请求网络,并解析音乐数据

时间:2016-04-30 08:59:00      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:

实现功能:
实现NetMusicListFragment(网络音乐界面)
实现net_music_list_layout.xml(网络音乐界面UI)
使用Jsoup组件请求网络,并解析音乐数据

截止到目前的源码下载:
http://download.csdn.net/detail/iwanghang/9506985

Jsoup组件导入:
AndroidStudio简单快速导入GitHub中的第三方组件 :
http://blog.csdn.net/iwanghang/article/details/51283763

Jsoup简介及中文API:
http://blog.csdn.net/iwanghang/article/details/51282403
http://blog.csdn.net/iwanghang/article/details/51283638
http://blog.csdn.net/iwanghang/article/details/51283659

欢迎移动开发爱好者交流:我的微信是iwanghang

另外,我打算开始找工作,如果沈阳或周边城市公司有意,也请与我联系。

实现效果如图:

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

实现代码如下:
NetMusicListFragment如下:
package com.iwanghang.drmplayer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.iwanghang.drmplayer.adapter.MyMusicListAdapter;
import com.iwanghang.drmplayer.utils.Constant;
import com.iwanghang.drmplayer.vo.Mp3Info;
import com.iwanghang.drmplayer.vo.SearchResult;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

public class NetMusicListFragment extends Fragment implements OnItemClickListener,OnClickListener {

    private ListView listView_net_music;

    private ArrayList<Mp3Info> mp3Infos;

    private LinearLayout load_layout;
    private LinearLayout ll_search_btn_container;
    private LinearLayout ll_search_container;
    private ImageButton ib_search_btn;
    private EditText et_search_content;

    private ArrayList<SearchResult> searchResults = new ArrayList<SearchResult>();

    private int page = 1;//搜索音乐的页码

    private MainActivity mainActivity;

    //private boolean isPause = false;//歌曲播放中的暂停状态

    private int position = 0;//当前播放的位置,提供给PlayActivity

    //onAttach(),当fragment被绑定到activity时被调用(Activity会被传入.).
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //mainActivity = (MainActivity) context;
        mainActivity = (MainActivity) getActivity();
    }

    public static NetMusicListFragment newInstance() {
        NetMusicListFragment net = new NetMusicListFragment();
        return net;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //UI组件初始化
        View view = inflater.inflate(R.layout.net_music_list_layout,null);
        //item
        listView_net_music = (ListView) view.findViewById(R.id.listView_net_music);
        //findViewById
        load_layout = (LinearLayout) view.findViewById(R.id.load_layout);
        ll_search_btn_container = (LinearLayout) view.findViewById(R.id.ll_search_btn_container);
        ll_search_container = (LinearLayout) view.findViewById(R.id.ll_search_container);
        ib_search_btn = (ImageButton) view.findViewById(R.id.ib_search_btn);
        et_search_content = (EditText) view.findViewById(R.id.et_search_content);

        //Item点击事件监听
        listView_net_music.setOnItemClickListener(this);
        //按钮点击事件监听
        ll_search_btn_container.setOnClickListener(this);
        ib_search_btn.setOnClickListener(this);
        loadNetData();//加载网络音乐
        return view;
    }

    private void loadNetData() {
        load_layout.setVisibility(View.VISIBLE);
        //加载网络音乐的异步任务
        new LoadNetDataTask().execute(Constant.BAIDU_URL + Constant.BAIDU_DAYHOT);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_search_btn_container:
                ll_search_btn_container.setVisibility(View.GONE);
                ll_search_container.setVisibility(View.VISIBLE);
                break;
            case R.id.ib_search_btn:
                //搜索事件
                searchMusic();
                break;
        }
    }

    //搜索音乐
    private void searchMusic() {

    }

    //加载网络音乐的异步任务
    class LoadNetDataTask extends AsyncTask<String,Integer,Integer>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            load_layout.setVisibility(View.VISIBLE);
            listView_net_music.setVisibility(View.GONE);
            searchResults.clear();
        }

        @Override
        protected Integer doInBackground(String... params) {
            String url = params[0];
            try {
                //使用Jsoup组件请求网络,并解析音乐数据
                Document doc = Jsoup.connect(url).userAgent(Constant.USER_AGENT).timeout(6*1000).get();
                Elements songTitles = doc.select("span.song-title");
                Elements artists = doc.select("span.author_list");
                System.out.println(artists);
                for (int i=0;i<songTitles.size();i++){
                    SearchResult searchResult = new SearchResult();
                    System.out.println(searchResult);
                    Elements urls = songTitles.get(i).getElementsByTag("a");
                    System.out.println(urls);
                    searchResult.setUrl(urls.get(0).attr("href"));
                    searchResult.setMusicName(urls.get(0).text());
                    //
                    Elements artistElements = artists.get(i).getElementsByTag("a");
                    System.out.println(artistElements);
                    searchResult.setArtist(artistElements.get(0).text());

                    searchResult.setAlbum("热歌榜");
                    searchResults.add(searchResult);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return -1;
            }
            return 1;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}

net_music_list_layout.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp">

    <LinearLayout
        android:id="@+id/ll_search_btn_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/tv_search_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableLeft="@drawable/search_button"
            android:text="搜索"
            android:textColor="#FF666666"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_search_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@null"
        android:orientation="horizontal"
        android:padding="5dp"
        android:visibility="gone">

        <EditText
            android:id="@+id/et_search_content"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@null"
            android:hint="搜索"
            android:padding="2dp"
            android:textColor="#FF666666"/>

        <ImageButton
            android:id="@+id/ib_search_btn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@null"
            android:padding="0dp"
            android:scaleType="centerInside"
            android:src="@drawable/search_button"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/load_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:visibility="gone">
        <ProgressBar
            android:id="@+id/pb_search_wait"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在努力搜索中"
            android:textSize="10sp"
            android:textColor="#8b8a8a"/>
    </LinearLayout>

    <ListView
        android:id="@+id/listView_net_music"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@android:color/transparent"
        android:overScrollMode="never"
        android:scrollbars="none"
        android:visibility="gone"/>
</LinearLayout>

SearchResult如下:

package com.iwanghang.drmplayer.vo;

/**
 * Created by iwanghang on 16/4/29.
 * 搜索音乐的对象
 */
public class SearchResult {
    private String musicName;
    private String url;
    private String artist;
    private String album;

    //set方法,get方法
    public String getMusicName() {
        return musicName;
    }

    public void setMusicName(String musicName) {
        this.musicName = musicName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    @Override
    public String toString() {
        return "SearchResult{" +
                "musicName='" + musicName + '\'' +
                ", url='" + url + '\'' +
                ", artist='" + artist + '\'' +
                ", album='" + album + '\'' +
                '}';
    }
}


Android开发本地及网络Mp3音乐播放器(十一)使用Jsoup组件请求网络,并解析音乐数据

标签:

原文地址:http://blog.csdn.net/iwanghang/article/details/51285328

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!