问题描述
我在Android应用一个简单的code和我在它使用一个简单的jsoup code连接到的链接,并获得在线广播的标题。但是当我点击播放按钮,我的应用程序退出与崩溃。这是我的onClick动作:
btnPlay.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
progressDialog.show(); 文档DOC = NULL;
尝试{
DOC = Jsoup.connect(\"http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm\").get();
。字符串s = doc.body()文本();
的System.out.println(多个);
lblMusicName.setText(多个);
}赶上(IOException异常五){
e.printStackTrace();
} 新主题(新的Runnable接口(){ @覆盖
公共无效的run(){ 如果(radioBtn128.isChecked())
URL =http://stream4.radiostyle.ru:8004/lezgifm;
否则如果(radioBtn32.isChecked())
URL =http://stream0.radiostyle.ru:8000/lezgifm;
setStream(URL);
IsPlaying模块= TRUE;
progressDialog.dismiss();
}
})。开始();
}
});
当我尝试调试,调试器中循环DOC文档=零点。有什么不好?
更新:
堆栈跟踪异常:
E / AndroidRuntime(880):致命异常:主要
E / AndroidRuntime(880):android.os.NetworkOnMainThreadException
E / AndroidRuntime(880):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
E / AndroidRuntime(880):在java.net.InetAddress.lookupHostByName(InetAddress.java:385)
E / AndroidRuntime(880):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E / AndroidRuntime(880):在java.net.InetAddress.getAllByName(InetAddress.java:214)
E / AndroidRuntime(880):在libcore.net.http.HttpConnection<&初始化GT;(HttpConnection.java:70)。
E / AndroidRuntime(880):在libcore.net.http.HttpConnection<&初始化GT;(HttpConnection.java:50)。
E / AndroidRuntime(880):在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340)
E / AndroidRuntime(880):在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
E / AndroidRuntime(880):在libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
E / AndroidRuntime(880):在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
E / AndroidRuntime(880):在libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
E / AndroidRuntime(880):在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
E / AndroidRuntime(880):在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
E / AndroidRuntime(880):在libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
E / AndroidRuntime(880):在org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:425)
E / AndroidRuntime(880):在org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:410)
E / AndroidRuntime(880):在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
E / AndroidRuntime(880):在org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
E / AndroidRuntime(880):在com.lezgifm.radio.MainActivity $ 1.onClick(MainActivity.java:109)
E / AndroidRuntime(880):在android.view.View.performClick(View.java:4202)
E / AndroidRuntime(880):在android.view.View $ PerformClick.run(View.java:17340)
E / AndroidRuntime(880):在android.os.Handler.handleCallback(Handler.java:725)
E / AndroidRuntime(880):在android.os.Handler.dispatchMessage(Handler.java:92)
E / AndroidRuntime(880):在android.os.Looper.loop(Looper.java:137)
E / AndroidRuntime(880):在android.app.ActivityThread.main(ActivityThread.java:5039)
E / AndroidRuntime(880):在java.lang.reflect.Method.invokeNative(本机方法)
E / AndroidRuntime(880):在java.lang.reflect.Method.invoke(Method.java:511)
E / AndroidRuntime(880):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
E / AndroidRuntime(880):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E / AndroidRuntime(880):在dalvik.system.NativeStart.main(本机方法)
您应该运行在解析的。不允许在UI线程上运行长时间操作。
把 Jsoup.connect()
在的AsyncTask
的 doInBackground()
方法,所以你不会得到 NetworkOnMainThreadException
。
i have a simple code in android app and I use in it a simple jsoup code for connection to the link and get online radio's title. but when i click on Play button, my app exit with crash. this is my onClick action:
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();
}
});
when i try to debug, the debugger is looping in Document doc = null point. what is wrong?
UPDATE:StackTrace Exception:
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)
You should run the parsing in an AsyncTask. It is not allowed to run long operation on UI thread.
Put the Jsoup.connect()
in AsyncTask
's doInBackground()
method, thus you won't get NetworkOnMainThreadException
.
这篇关于使用Jsoup Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!