我正在尝试解析 .pls 文件以获取要播放的网址。我使用了以下代码,但它给出了 networkonmainthread 异常。我以前从未做过线程应用程序;我怎样才能在新线程上运行这个类?
public class GetStreamingUrl {
private static String LOGTAG = "GetStreamingUrl";
private Context mContext;
public String url1;
public LinkedList<String> url2;
public GetStreamingUrl(Context context) {
Log.i(LOGTAG, "call to constructor");
this.mContext = context;
}
public LinkedList<String> getStreamingUrl(String url) {
Log.i(LOGTAG, "get streaming url");
final BufferedReader br;
String murl = null;
LinkedList<String> murls = null;
try {
URLConnection mUrl = new URL(url).openConnection();
br = new BufferedReader(
new InputStreamReader(mUrl.getInputStream()));
murls = new LinkedList<String>();
while (true) {
try {
String line = br.readLine();
if (line == null) {
break;
}
murl = parseLine(line);
if (murl != null && !murl.equals("")) {
murls.add(murl);
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(LOGTAG, "url to stream :" + murl);
return murls;
}
private String parseLine(String line) {
if (line == null) {
return null;
}
String trimmed = line.trim();
if (trimmed.indexOf("http") >= 0) {
return trimmed.substring(trimmed.indexOf("http"));
}
return "";
}
}
我正在尝试解析 .pls 文件以获取要播放的网址。我使用了以下代码,但它给出了 networkonmainthread 异常。我以前从未做过线程应用程序;我怎样才能在新线程上运行这个类?