title: How To Extract Table Data Via Android
body: <p>I'm attempting to extract data from a table on a web page. Thus far I've been able to extract the data from the title tag - but not the table data. How can this be accomplished using the source shown below?</p>
  
  <p>SOURCE:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  TextView tv;
  final String URL="http://example.com";
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  tv = (TextView) findViewById(R.id.TextView01);
  new MyTask().execute(URL);
  }
  
  private class MyTask extends AsyncTask&lt;String, Void, String&gt; {
  ProgressDialog prog;
  String title = "";
  @Override
  protected void onPreExecute() {
        prog = new ProgressDialog(MainActivity.this);
        prog.setMessage("Loading....");
        prog.show();
  }
  @Override
  protected String doInBackground(String... params) {
      try {
          Document doc = Jsoup.connect(params[0]).get();
          title = doc.title();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return title;
  }
  @Override
  protected void onPostExecute(String result) {
      super.onPostExecute(result);
      prog.dismiss();
      tv.setText(result);
  }
  
  }
  }
  </code></pre>
  
  <p>EDIT - In Response to Matej Spili's comment below:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  TextView tv;
  final String URL="http://example.com";
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  tv = (TextView) findViewById(R.id.TextView01);
  new MyTask().execute(URL);
  }
  
  private class MyTask extends AsyncTask&lt;String, Void, String&gt; {
  ProgressDialog prog;
  String title = "";
  @Override
  protected void onPreExecute() {
        prog = new ProgressDialog(MainActivity.this);
        prog.setMessage("Loading....");
        prog.show();
  }
  @Override
  protected String doInBackground(String... params) {
      try {
          Document doc = Jsoup.connect(params[0]).get();
          Element tableHeader = doc.select("tr").first();
  
  
          for( Element element : tableHeader.children() )
          {
              // Here you can do something with each element
              System.out.println(element.text());
              tv.setText(element.text());
          }
  
          title = doc.title();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return title;
  }
  @Override
  protected void onPostExecute(String result) {
      super.onPostExecute(result);
      prog.dismiss();
      tv.setText(result);
  }
  }
  }
  </code></pre>
  
  <p>^- (causes error: <a href="https://stackoverflow.com/questions/18837665/android-view-viewrootimplcalledfromwrongthreadexception-only-the-original-thre">android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views</a>)</p>