Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發本地及網絡Mp3音樂播放器(十一)使用Jsoup組件請求網絡,並解析音樂數據

Android開發本地及網絡Mp3音樂播放器(十一)使用Jsoup組件請求網絡,並解析音樂數據

編輯:關於Android編程

實現功能:
實現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 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 searchResults = new ArrayList();

    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{
        @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 parent, View view, int position, long id) {

    }
}

net_music_list_layout.xml如下:
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:paddingleft="5dp" android:paddingright="5dp" android:paddingtop="5dp" xmlns:android="http://schemas.android.com/apk/res/android">

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

        <textview android:drawableleft="@drawable/search_button" android:id="@+id/tv_search_show" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="搜索" android:textcolor="#FF666666">
    </textview></linearlayout>

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

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

        <imagebutton android:background="@null" android:id="@+id/ib_search_btn" android:layout_height="match_parent" android:layout_width="wrap_content" android:padding="0dp" android:scaletype="centerInside" android:src="@drawable/search_button">
    </imagebutton></edittext></linearlayout>

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

    <listview android:cachecolorhint="@android:color/transparent" android:id="@+id/listView_net_music" android:layout_height="wrap_content" android:layout_width="match_parent" android:listselector="@android:color/transparent" android:overscrollmode="never" android:scrollbars="none" android:visibility="gone">
</listview></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 + '\'' +
                '}';
    }
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved