Android搜索并下载歌词,thanks to www.lyricist.cn!

在android上,下面这段代码可以从乐辞服务器上抓取歌词并保存到本地,请不要用于商业目的!

http内容获取可以用多种方式,下面的代码使用HttpGet和URLConnection两种方式都可以奏效。

LrcCatcherLyricist.java
  1 import java.io.File;
2 import java.io.FileWriter;
3 import java.io.IOException;
4
5 import org.apache.http.HttpResponse;
6 import org.apache.http.HttpStatus;
7 import org.apache.http.client.HttpClient;
8 import org.apache.http.client.methods.HttpGet;
9 import org.apache.http.impl.client.DefaultHttpClient;
10 import org.apache.http.util.EntityUtils;
11
12 import android.util.Log;
13
14 public class LrcCatcherLyricist {
15 public String find(String ar, String ti, String store) {
16 String ret = null;
17 HttpClient httpClient = new DefaultHttpClient();
18
19 String sUrl = "http://www.winampcn.com/lrceng/get.aspx?song={ti}&artist={ar}&lsong={lti}&prec=1&Datetime=20060601";
20 sUrl = sUrl.replace("{ti}", ti).replace("{ar}", ar)
21 .replace("{lti}", ti);
22 Log.d("LrcCatcherLyricist Get", sUrl);
23
24 /**
25 * first way: using HttpGet.
26 */
27 HttpGet httpGet = new HttpGet(sUrl);
28 // set parameters.
29 // HttpParams httpParams = new BasicHttpParams();
30 // HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
31 // HttpConnectionParams.setSoTimeout(httpParams, 30000);
32 // httpGet.setParams(httpParams);
33 try {
34 HttpResponse response = httpClient.execute(httpGet);
35 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
36 ret = EntityUtils.toString(response.getEntity());
37 }
38 } catch (Exception e) {
39 return ret;
40 }
41 MusicTracker.d("LrcCatcherLyricist", ret);
42
43 /**
44 * second way: using URLConnection.
45 */
46 // a
47 // URL url = new URL(sUrl);
48 // URLConnection conn = url.openConnection();
49 //
50 // BufferedReader rd = new BufferedReader(new InputStreamReader(
51 // conn.getInputStream()));
52 // String line = "";
53 //
54 // while ((line = rd.readLine()) != null) {
55 // Log.d("LrcCatcherLyricist Get", line);
56 // }
57
58 /**
59 * get lyric down-load URL. just only get the first result.
60 * [CDATA[http://www.lyricist.cn/lrceng/lrc.aspx?id={id}&ti={ti}]]
61 */
62 // find "[CDATA[" and get lyric URL.
63 String sflag = "[CDATA[";
64 String eflag = "]]";
65 int s = ret.indexOf(sflag);
66 int e = ret.indexOf(eflag, s);
67 ret = ret.substring(s + sflag.length(), e);
68
69 /**
70 * write lyric to file system.
71 */
72 if (lrcDownload(ret, store)) {
73 return null;
74 } else {
75 return ret;
76 }
77 }
78
79 private boolean lrcDownload(String sUrl, String store) {
80 HttpClient httpClient = new DefaultHttpClient();
81 String ret = null;
82
83 Log.d("LrcCatcherLyricist", "lrcDownload:" + sUrl);
84
85 HttpGet httpGet = new HttpGet(sUrl);
86 try {
87 HttpResponse response = httpClient.execute(httpGet);
88 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
89 ret = EntityUtils.toString(response.getEntity());
90 }
91 } catch (Exception e) {
92 return false;
93 }
94
95 File lrc = new File(store);
96 try {
97 lrc.createNewFile();
98 FileWriter fw = new FileWriter(lrc);
99 fw.write(ret);
100 fw.flush();
101 fw.close();
102 return true;
103 } catch (IOException e) {
104 e.printStackTrace();
105 }
106
107 return false;
108 }
109 }

闲来无事写的,不要用于商业目的。谢谢合作~~~

posted @ 2012-02-03 14:20  fuck.cao  阅读(395)  评论(0编辑  收藏  举报