2013-03-11 39 views
-1

這裏是我的代碼部分:jsoup並不在我的Android應用程序的工作,總是得到錯誤使用get()

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,它的作品。所以哪裏出錯?

+0

後的堆棧跟蹤。也似乎你在主線程中做網絡操作,導致了'NetworkOnMainThreadException.'不要那樣做。 – 2013-03-11 15:55:15

+0

-1 A /不張貼堆棧跟蹤。 B /沒有定義「錯誤」C /沒有搜索到異常。 – njzk2 2013-03-11 16:12:28

回答

1

你好,我在我的應用程序也有這個。

的解決方法是相當簡單:

所有Jsoup行動在Asyntask做或螺紋

我個人使用的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; 
} 
0

代碼之前,我不知道,但你沒有做的活動的任何連接,調用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(); 
} 
相關問題