1

I'm trying to parsing this website: http://sbaftv.superdriver.it/ro/asp/ricercaorari.asp?user=FTV1&CodProfilo=1&VisInternet=1&visnota=0 in particular way the options in Comune di Partenza and Comune di Arrivo but i can't do it. This is the code:

public class MainActivity extends Activity {

    ListView lista;
    static final String BLOG_URL = "http://sbaftv.superdriver.it/ro/asp/ricercaorari.asp?user=FTV1&CodProfilo=1&VisInternet=1&visnota=0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lista = (ListView)this.findViewById(R.id.main_lista);//recupero lista da id

        //creo ed eseguo l'asynctask
        ParsingPaginaWeb parsing = new ParsingPaginaWeb();
        parsing.execute("");
    }

    private class ParsingPaginaWeb extends AsyncTask<String,String,String> {

        ArrayList<String> titoli; //lista dei titoli
        //ArrayList<String> descrizioni; //lista delle descrizioni

        @Override
        protected void onPreExecute()
        {
            //prima di eseguire il parsing inizializzo gli arraylist
            titoli = new ArrayList<String>();
            //descrizioni = new ArrayList<String>();
        }

        @Override
        protected String doInBackground(String... params) {
            try {

                Document doc = Jsoup.connect(BLOG_URL).get(); //E' il sito dove faccio il parsing   
                // prelevo l'h3 cioè il titolo di ogni sezione e poi ciclo tutto
                Elements nodeBlogStats = doc.select("div.comunePartINI");
                for(Element sezione : nodeBlogStats)//per ogni sezione tra gli elementi ricavati prima
                {

                        titoli.add(sezione.text());
                        //descrizioni.add(descrizione);

                }
            } catch (Exception e) {
                // In caso di errore
                Log.e("ESEMPIO", "ERRORE NEL PARSING");
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result)
        {
            // dopo che ho eseguito il parsing mostro i dati nella listview
            // usando il custom array adpater ParsingArrayAdapter
            ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli);
            lista.setAdapter(adapter);
        }

    }

}

the application display a empty activity. Any ideas?

David_D
  • 1,404
  • 4
  • 31
  • 65

2 Answers2

1

That's because you are not parsing data correctly. It is returning zero elements. I have modified you parsing code. The following code extracts all the options of Comune di Partenza.

Use the following code in your try block

doc = Jsoup.connect(BLOG_URL).get();
Elements nodeBlogStats = doc.select("#comunePartINI > option");
for(Element sezione : nodeBlogStats)
{
    titoli.add(sezione.text());
}

Also its a good practice to return the result from asynctask and use it in onPostExecute rather than defining a field of a class and then modifying it.

Anurag
  • 1,521
  • 2
  • 17
  • 34
  • Yeah it works!!! Amazing! Thank you.. do you think it will be possible parsing everything in that page to create an application? – David_D Jan 09 '14 at 13:22
  • Whatever information available in the source code can be parsed easily by jsoup. And as long as data is available, you can use it in whatever way you want in your application. Do check any copyright violation for that website which you are using. – Anurag Jan 09 '14 at 13:59
  • I can give you some suggestions on how to proceed, but any programming stuff you only have to do. In case you find any problems just ask here on stackoverflow. – Anurag Jan 10 '14 at 17:07
  • Hi, can you help me to parse the `

    ` tag in this webpage? http://multiplayer.it/notizie/127536-shadow-blade-shadow-blade-debutta-oggi-su-app-store.html using my code?

    – David_D Jan 16 '14 at 23:37
  • Use this Elements e=doc.select("p"); And be more specific, which

    tags exactly? Anyway I guess the above will get you started.

    – Anurag Jan 17 '14 at 18:53
  • Look at this http://stackoverflow.com/questions/21181685/how-parse-image-with-jsoup?noredirect=1#comment31890231_21181685 here i want load the image for each article of that webpage and load it in the listview.. Of course i can't do it.. The only one thing i can is display the url of images. Anyway, really i see that you know this argoument so please can you gettin in touch with me to help me? – David_D Jan 19 '14 at 11:31
0

Your selector is trying to retrieve a 'div' node with a 'comunePartINI' class attribute. I think you really need to retrive are the 'option' nodes contained in a 'select' node with a 'comunePartINI' id attribute:

Elements nodeBlogStats = doc.select("select#comunePartINI > option");
for(Element sezione : nodeBlogStats)//per ogni sezione tra gli elementi ricavati prima
{
    titoli.add(sezione.text());
}

Regards.

eltabo
  • 3,749
  • 1
  • 21
  • 33