2013-03-11 40 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(); 
} 
相关问题