-2

I am trying to parse .pls file to get urls to play. I used following code, but it is giving networkonmainthread exception. I never did threaded apps before; How can I run this class on new thread?

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 "";
    }



}

I am trying to parse .pls file to get urls to play. I used following code, but it is giving networkonmainthread exception. I never did threaded apps before; How can I run this class on new thread?

Wasif Laeeq
  • 138
  • 1
  • 2
  • 11

2 Answers2

3

Just do this:

new Thread(new Runnable() {

            @Override
            public void run() {
                // DO YOUR STUFFS HERE

            }
        }).start();

Hope this helps.

user2652394
  • 1,686
  • 1
  • 13
  • 15
0

You can use AsyncTask and do the work inside it. Follow the link below for more details:

http://developer.android.com/reference/android/os/AsyncTask.html

Sushil
  • 8,250
  • 3
  • 39
  • 71