这是我的代码的一部分:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lyrics_display);
setupActionBar();
TextView ArtistName = (TextView)findViewById(R.id.aname);
TextView SongName = (TextView)findViewById(R.id.sname);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE2);
// Create the text view
ArtistName.setTextSize(12);
ArtistName.setText(message);
SongName.setTextSize(12);
SongName.setText(message2);
getLyrics(message , message2);
}
@SuppressLint("DefaultLocale")
public void getLyrics(String message , String message2) {
final TextView LyricsContent = (TextView)findViewById(R.id.lyricscontent);
Document doc = null;
String url = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist="+message.toLowerCase()+"&song="+message2.toLowerCase();
LyricsContent.setText(url);
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
输入后总是停止(String message,String message2)。 我已经用它了:android.permission.INTERNET android.persmission.ACCESS_NETWORK_STATE />
当我删除jsoup的部分时,它可以工作。那么哪里出错?
答案 0 :(得分:1)
您好我的申请表中也有这个。
解决方案非常简单:
所有Jsoup动作都必须在Asyntask或Thread
中完成我个人使用像这样的Asynctask:
在活动
之前的代码顶部有一个String歌词 private class LoadLyric extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// Here you can do any UI operations like textview.setText("test");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
Document doc = null;
try {
doc = Jsoup.connect(url).get();
lyrics = doc.text(); // or atleast do something like doc.getElementsByTag("Lyric"); in your case
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
答案 1 :(得分:0)
我不确定,但您不必在Activity中建立任何连接,并调用getLyrics()。
@SuppressLint("DefaultLocale")
public void getLyrics(String message , String message2) {
new Thread(new Runnable() {
public void run() {
final TextView LyricsContent = (TextView)findViewById(R.id.lyricscontent);
Document doc = null;
String url = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist="+message.toLowerCase()+"&song="+message2.toLowerCase();
LyricsContent.setText(url);
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}