在Android中使用Jsoup

时间:2013-05-20 14:11:18

标签: android jsoup

我在Android应用程序中有一个简单的代码,我在其中使用一个简单的jsoup代码连接到链接并获得在线广播的标题。但当我点击播放按钮时,我的应用程序退出崩溃。这是我的onClick动作:

btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            progressDialog.show();

            Document doc = null;
            try {
                doc = Jsoup.connect("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm").get();
                String s = doc.body().text();
                System.out.println(s);
                lblMusicName.setText(s);
            } catch (IOException e) {
                e.printStackTrace();
            }

            new Thread(new Runnable() {

                @Override
                public void run() { 

                    if (radioBtn128.isChecked())
                        url = "http://stream4.radiostyle.ru:8004/lezgifm";
                    else if (radioBtn32.isChecked())
                        url = "http://stream0.radiostyle.ru:8000/lezgifm";
                    setStream(url);
                    isPlaying = true;
                    progressDialog.dismiss();
                }
            }).start();             
        }
    });

当我尝试调试时,调试器在Document doc = null point中循环。怎么了?

更新: StackTrace异常:

E/AndroidRuntime(880): FATAL EXCEPTION: main
E/AndroidRuntime(880): android.os.NetworkOnMainThreadException
E/AndroidRuntime(880):  at  android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
E/AndroidRuntime(880):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
E/AndroidRuntime(880):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/AndroidRuntime(880):  at java.net.InetAddress.getAllByName(InetAddress.java:214)
E/AndroidRuntime(880):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
E/AndroidRuntime(880):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
E/AndroidRuntime(880):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
E/AndroidRuntime(880):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
E/AndroidRuntime(880):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
E/AndroidRuntime(880):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
E/AndroidRuntime(880):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
E/AndroidRuntime(880):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
E/AndroidRuntime(880):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
E/AndroidRuntime(880):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
E/AndroidRuntime(880):  at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
E/AndroidRuntime(880):  at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
E/AndroidRuntime(880):  at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
E/AndroidRuntime(880):  at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
E/AndroidRuntime(880):  at com.lezgifm.radio.MainActivity$1.onClick(MainActivity.java:109)
E/AndroidRuntime(880):  at android.view.View.performClick(View.java:4202)
E/AndroidRuntime(880):  at android.view.View$PerformClick.run(View.java:17340)
E/AndroidRuntime(880):  at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(880):  at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(880):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(880):  at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(880):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(880):  at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(880):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(880):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(880):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您应该在AsyncTask中运行解析。不允许在UI线程上运行长操作。

Jsoup.connect()置于AsyncTask的{​​{1}}方法中,这样您就不会获得doInBackground()