0

I would like to retrieve data contained in the tables from a website http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx and use it in an android application so that when updated, the information in the application is also updated. However, i am not very familar with android though. So i need help to retrieve data using htmlcleaner and jsoup/json.

Thanx.

Kinny
  • 21
  • 4

2 Answers2

1
public class MainActivity extends Activity {

TextView tv; final String URL = "http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView) findViewById(R.id.textView);
    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();
            //timeout(90000).ignoreHttpErrors(true);

            //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;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        tv.setText(title);
        prog.dismiss();
    }
        }
@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;
}

}

Kinny
  • 21
  • 4
  • Why did u comment ur timeout. Maybe thats the cause. Also just put the URL directly to Jsoup.connect(http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx) except its coming from user input just put it directly and do this on ur post execute if (this.prog.isShowing()) { this.prog.dismiss(); }. Also set this as another question entirely too. – ImGeorge Mar 20 '14 at 13:56
  • I put back the timeout. The program runs but does not dispaly anything at the end of it. Im going to set it as another question. Thanx. – Kinny Mar 20 '14 at 15:57
  • Send me the link once u set it.. also did u put the url directly – ImGeorge Mar 20 '14 at 15:59
  • Yeah, i inserted the url directly. – Kinny Mar 20 '14 at 16:01
  • am checking it now, it looks like the problem is from the query or the site if u change the loop to doc.select(html) u see that it prints something. ur code is fine just pay attention to the css query or jquery – ImGeorge Mar 20 '14 at 16:34
  • Ok..i've come up with another question. Here is the link.[http://stackoverflow.com/questions/22539554/how-to-retrieve-table-data-from-a-website-and-display-it-in-a-textview-in-androi] – Kinny Mar 20 '14 at 16:52
0

MTN yello lol. used it in Nigeria. below is a sample on how to do it assuming you knw android already then you need to go to http://jsoup.org/cookbook/ to learn more about jsoup libraries

 TextView textView;
    Document doc = Jsoup.connect("http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx").timeout(90000)
                                .ignoreHttpErrors(true).get();

        for (Element Yello: doc.select("div tbody:contains(Bundle):eq(6) tr td") )) {

    textView.setText(Yello.text());

                        }

Good luck man

ImGeorge
  • 527
  • 6
  • 24
  • Thanks George for the help. But the app times out everytime i run. Could also show asynchTask. – Kinny Mar 19 '14 at 15:59
  • Ok That will be a different question, please mark this as answer if it help you. Hints, you need to put it inside asyntask method to handle the dat been downloaded from the site incase the internet is slow or the website has so much data on the table. Also Increase the timeout request. Check this out too-- http://stackoverflow.com/questions/7083680/how-to-use-asynctask-for-jsoup-parser. Thanks – ImGeorge Mar 19 '14 at 19:02
  • My code is attached above but i dont get results displayed. – Kinny Mar 20 '14 at 06:21
  • I am still struggling with the display. @ George – Kinny Mar 20 '14 at 07:47