java - Android htmlcleaning -


I use this code to read an html page, but I have an error ... how do I solve it Do you?

Error:

  Fatal exception: main process: com.example.nuovo, PID: 1615 java.lang.RuntimeException: Unable to initiate the activity ComponentInfo {com. example. nuovo / com.example.nuovo.Nuovo_Activity}: android.os.NetworkOnMainThreadExceptionAndroid.app.activatrid.form launchery (activityTrad.sgans1919) android App ActivitivedAndal Launch Activity (ActivityTrad.Java 2245) on Android.AP .ActivityThread.access $ 800 (ActivityThread.java:135)   

Code:

  Package Com.example.nuovo; Import java.io.IOException; Import java.net.MalformedURLException; Import java.net.URL; Import org.htmlcleaner.CleanerProperties; Import org.htmlcleaner.HtmlCleaner; Import org.htmlcleaner.TagNode; Import org.htmlcleaner.XPather Exceptions; Import android.util.log; Public class test {public string getRis () throws malformedlexception, IOException, XPatherException {string result = "aa"; Log D. ("Proge di mazeszonanto", "test"); Cleaner Properties Props = New Cleaner Properties (); // Set some properties for non-basic values ​​propssetTranslateSpecialEntities (true); Props.setTransResCharsToNCR (right); props.setOmitComments (true); URL url = new URL ("https ........... aspx"); Log D. ("Prava de franciumant post 1", "test"); // do tag node tagNode = new HtmlCleaner (PRO) parsing. Kalan (url); Log D. ("Prava de mazzyndopost 2", "test"); Object [] nodes = tag node.valuexpath ("// table [@ class = 'tablequartzian']"); Tag node node = (tag node) nodes [0]; Nodes = node.value xpath ("// td"); Log D. ("Prava de Franzionondo 2", "Test"); For (int i = 1; i & lt; nodes.length; i ++) {string ris = (TagNode nodes [i]). GetText ()). ToString (); = RIS result; } Return results; In Android you can not work with the Internet in the main thread, so why? You have NetworkOnMainThreadException to use AsyncTask to work with the network Here is a simple example of the AsyncTask class.  
  Class MyAsyncTask asyncTask and lt; Zero, zero, zero> {myAsyncTask () {} // // is executed on UI thread before taking time @overridePreExecute protected (protected) {super.onPreExecute (); } // executed on a special thread and at the time of taking all your time / work, there should be @ Override protected voice downline background (zero ... request) inside the method {// work on your network, return tap here ; } // // After the timeout process is completed, the UI is executed on the thread @Override Secure Zero at postexec (zero result) {// Update your ideas here. SuperPointExecute (results); }}}   

To begin using the job:

  MyAsyncTask mTask = New MyAsyncTask (); MTask.execute ();    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -