-1

这是我的代码的一部分:

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();
    }           
}

它总是在我输入(字符串消息,字符串消息2)后停止。我已经使用它了:android.permission.INTERNET android.persmission.ACCESS_NETWORK_STATE/>

当我删除 jsoup 的部分时,它可以工作。那么哪里出了问题?

4

2 回答 2

1

您好,我的申请中也有这个。

解决方案相当简单:

所有 Jsoup 动作都必须在 Asyntask 或 Thread 中完成

我个人使用这样的 Asynctask:

活动前在代码顶部有一个字符串歌词

    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;
}
于 2013-03-11T17:41:23.473 回答
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();
}
于 2014-07-17T22:53:47.003 回答