1

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?

SOURCE:

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<String, Void, String> {
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);
}

}
}

EDIT - In Response to Matej Spili's comment below:

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<String, Void, String> {
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);
}
}
}

^- (causes error: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views)

Community
  • 1
  • 1
OhNoItsAnOverflow
  • 237
  • 2
  • 5
  • 13
  • here is your answer http://stackoverflow.com/questions/13545658/extract-data-out-of-table-with-jsoup – Matej Špilár Sep 16 '13 at 21:15
  • Thanks! P.S. If you want another easy question to answer - I have another (related) issue! : ) http://stackoverflow.com/questions/18904302/unable-to-parse-tablerow-data – OhNoItsAnOverflow Sep 19 '13 at 21:00

1 Answers1

0

Pass the view that you want to update as constructor argument for the MyTask like

new MyTask(tv).execute(URL);

Change your MyTask accordingly to accept this argument.

Manoj Srivatsav
  • 280
  • 2
  • 11