如何从网站检索表格数据并在Android的文本视图中显示

时间:2014-03-20 16:39:14

标签: android jsoup textview

我正在开发一个简单的Android应用程序,但卡住了。我需要帮助从this website上的表中检索数据并将其显示在TextView中。以下是我尝试使用的代码:

 public class MainActivity extends Activity {
    TextView tv;
    final String URL = "http://mtn.co.ug/MTN-Internet/MTN-Mobile-Internet.aspx";

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv = (TextView) findViewById(R.id.textView);
            new MyTask().execute();
    }

    private class MyTask extends AsyncTask<Void, Void, Void> {
            ProgressDialog prog;
            //String title = "";
            @Override
            protected void onPreExecute() {
                prog = new ProgressDialog(MainActivity.this);
                prog.setMessage("Loading....");
                prog.show();
            }
            @Override   
            protected Void doInBackground(Void... params) {
                try {
                    Document Doc= Jsoup.connect("http://mtn.co.ug/MTN-Internet/MTN-Mobile-Internet.aspx").timeout(90000).ignoreHttpErrors(true).get();

                    /*for (Element table : Doc.select("tabscontent")) {
                    for (Element row : table.select("tr")) {
                        Elements tds = row.select("td");
                        if (tds.size() > 6) {
                            System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
                        }
                    }
                }

                */          

                //title = Doc.title();
                for (Element Yello: Doc.select("div tbody:contains(Bundle):eq(6) tr td") ) {
                    System.out.println(Yello.text());
                    tv.setText(Yello.text());

                    //title = Yello.toString();
                } 
            } catch (IOException e) {
                e.printStackTrace();
            }
            //return null;
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (this.prog.isShowing()) { 
                this.prog.dismiss(); 
            }
            //tv.setText(title);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

试一下

Document doc = Jsoup.connect("http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
                    .timeout(9999999).followRedirects(true).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").get();;


        for (Element Yello: doc.select("div:contains(bundle) tr") ) {

            String results = Yello.select("td").text().trim();
           tv.setTxt(results)


                                }

    }