在Android上解析SHOUTcast 7.html元数据

时间:2014-02-13 22:34:35

标签: android html parsing shoutcast

我正在尝试使用以下网址检查SHOUTcast流的状态:

http://85.17.167.136:8684/7.html

...返回如下数据:

<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>7,1,77,100,7,128,+44(0)7908 340 811 Follow Us @visionradiouk</body></html>

我知道如果流已启动并正在运行,则第一个逗号之后返回1;如果流已关闭,则返回0。我的问题是获取该页面的HTML?我使用此代码,适用于谷歌等其他网站。

    TextView tView = (TextView) findViewById(R.id.textView1);


String htmlCode = "";

try {
    URL url = new URL("http://85.17.167.136:8684/7.html"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    String inputLine;

    while ((inputLine = in.readLine())!= null)
        htmlCode += inputLine;
    System.out.println(htmlCode);
    tView.setText(htmlCode);
    in.close();     
} catch (Exception e){
    System.out.println("error");
}


}

关于我做错的任何想法?

1 个答案:

答案 0 :(得分:1)

Heres Pulsarman325的工作解决方案,整理了一些额外的东西,我必须添加它才能让它工作(尝试/捕获和变量初始化)

String url = "http://molestia.ponify.me:8062";

URL url2=null;

try
{
    url2 = new URL(url + "/7.html");

}

catch (MalformedURLException e1)
{
    e1.printStackTrace();
}

URLConnection con=null;

try
{
    con = url2.openConnection();
}

catch (IOException e1)
{
  e1.printStackTrace();

}

con.setRequestProperty("User-Agent", "Mozilla/5.0");

Reader r = null;

try
{
    r = new InputStreamReader(con.getInputStream());
}

catch (IOException e)
{
  e.printStackTrace(); 
}

StringBuilder buf = new StringBuilder();

int ch=0;

while (true)
{
    try
    {
        ch = r.read(); 
    }

    catch (IOException e)
    {
      e.printStackTrace();  
    }

    if (ch < 0)
      break;

    buf.append((char) ch);

}

String str = buf.toString();

String trackinfo = str.split(",")[6].split("</body>")[0];

Log.d("HTML", trackinfo);