0

im try to load dynamic certain contents of a website into webview but i cant i use this code `public class MainActivity extends Activity {

// blog url
static final String BLOG_URL = "http://www.internationalnewscenter.com/";

@Override
public void onCreate(Bundle savedInstanceState) {
    // set layout view
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // process
    try {
        ((TextView)findViewById(R.id.textView1)).setText(getBlogStats());
    } catch (Exception ex) {
        ((TextView)findViewById(R.id.textView1)).setText("Error");
    }
}

protected String getBlogStats() throws Exception {
    String result = "";
    // get html document structure
    Document document = Jsoup.connect(BLOG_URL).get();
    // selector query
    Elements nodeBlogStats = document.select("div#lofslidecontent45");
    // check results
    if(nodeBlogStats.size() > 0) {
        // get value
        result = nodeBlogStats.get(0).text();
    }

    // return
    return result;
}

} but it display for me an " error "` is any one can help me or give me a link for full example to do that

DoctorDoom
  • 501
  • 2
  • 8
  • 21
  • add ex.printstacktrace(); and tell me what's showing up the console – Xenione Sep 23 '13 at 10:26
  • where i should add this – DoctorDoom Sep 23 '13 at 10:30
  • } catch (Exception ex) { ex.printstacktrace(); ((TextView)findViewById(R.id.textView1)).setText("Error"); } – Xenione Sep 23 '13 at 10:34
  • anyway I recomend you to use a webservice instead – Xenione Sep 23 '13 at 10:37
  • [2013-10-01 13:45:35 - international] Installing international.apk... [2013-10-01 13:45:52 - international] Success! [2013-10-01 13:45:52 - international] Starting activity com.example.international.MainActivity on device YT9100KHZ4 [2013-10-01 13:45:54 - international] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.international/.MainActivity } – DoctorDoom Sep 23 '13 at 10:46
  • do youy have an example do this task using webservice ??? please help me – DoctorDoom Sep 23 '13 at 11:11
  • add just the exception is in orange but probably you hyave got the exception bellow that takaki point out, follow the suggestion to pass through – Xenione Sep 23 '13 at 11:28
  • you need to buid an script in the server side, I don't know if it's possible in your case, otherwise you are parsing a website that it may change and your app stops working – Xenione Sep 23 '13 at 11:31

1 Answers1

1

It will throw a NetworkOnMainThreadException if you call Jsoup.connect on the main thread.

Try using an AsyncTask to connect to the blog, retrieve the content, and then set it to display in the TextView.

See the selected answer here for a good example of this.

Also, don't forget to add the INTERNET permission to your manifest!

<uses-permission android:name="android.permission.INTERNET" /> 
Community
  • 1
  • 1
ashatte
  • 5,442
  • 8
  • 39
  • 50