Jsoup doesn’t seem to be working

Asked

Viewed 38 times

1

Hello I have this piece of code that gives a link and that goes online with jsoup takes some information and then puts that information in an Arraylist used in a Listview. But the problem is that I don’t know why Array courses aren’t full, the site changes depending on the letter in the url so I do this code. I do not understand why and that it does not fill the variable courses of . Lin-area-C2 that are on the site. Here is the code :

  public class Chose_curso extends AppCompatActivity {

private TextView pickLetraText;
private ListView cursoList;
private Spinner letraPicker;

private ArrayList<String> letras = new ArrayList<String>();
private ArrayList<String> cursos = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chose_curso);


    pickLetraText = (TextView) findViewById(R.id.pickLetraText);
    cursoList = (ListView) findViewById(R.id.cursoList);
    letraPicker = (Spinner) findViewById(R.id.letraPicker);

    letras.add("A");
    letras.add("B");
    letras.add("C");
    letras.add("D");
    letras.add("E");
    letras.add("F");
    letras.add("G");
    letras.add("H");
    letras.add("I");
    letras.add("J");
    letras.add("L");
    letras.add("M");
    letras.add("N");
    letras.add("O");
    letras.add("P");
    letras.add("Q");
    letras.add("R");
    letras.add("S");
    letras.add("T");
    letras.add("U");
    letras.add("V");




    ArrayAdapter<String> adapterLetras = new ArrayAdapter<String>(Chose_curso.this,android.R.layout.simple_spinner_item,letras);

    adapterLetras.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    letraPicker.setAdapter(
            new NothingSelectedSpinnerAdapter(
                    adapterLetras,
                    R.layout.contact_spinner_row_nothing_selected,
                    // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
                    this));

    Document document = null;



    letraPicker.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            setCursoList();
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }


        });}


public void setCursoList(){
            try  {
                if(letraPicker.getSelectedItem()!=null){
                    cursos.clear();
                    Document document = null;

                    String site ="http://www.dges.gov.pt/guias/indcurso.asp?letra="+(letraPicker.getSelectedItem().toString());

                    document = Jsoup.connect(site).get();
                    for(int contador=0;contador<document.select(".lin-area-c2").size();contador++){
                        cursos.add(String.valueOf((document.select(".lin-area-c2").get(contador).text())));}}
            } catch (Exception e) {
                e.printStackTrace();
            }






    ArrayAdapter<String> cursosAdapter = new ArrayAdapter<String>(Chose_curso.this,android.R.layout.simple_spinner_item,cursos);

    cursoList.setAdapter(cursosAdapter);


}

}

Website link http://www.dges.gov.pt/guias/indcurso.asp?letra=B in this case with the letter B selected . What I’m doing is basically giving the user the opportunity to choose which letter and what they want in the url when I fetch the information!

To resolve I did this way as indicated :

  public List<String> buscarCursos(final String letra) throws IOException {

    final List<String> cursos1 = new ArrayList<>();

    final ArrayAdapter<String> cursosAdapter = new ArrayAdapter<String>(Chose_curso.this,android.R.layout.simple_spinner_item,cursos1);

    cursoList.setAdapter(cursosAdapter);

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try  {
                String site = "http://www.dges.gov.pt/guias/indcurso.asp?letra=";
                Document document;

                Elements lista;

                document = Jsoup.connect(site +letra).get();

                lista = document.select(".lin-area-c2");

                for (Element elemento: lista) {
                    cursos1.add(elemento.text());
                }



            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();


    return cursos1;

}

I used the search for courses to withdraw all courses for each letter as the answer that was indicated to me.

1 answer

1


You can use the following method to find all course descriptions:

public List<String> buscarCursos(String letra) throws IOException {
  String site = "http://www.dges.gov.pt/guias/indcurso.asp?letra=";
  List<String> cursos = new ArrayList<>();
  Document document;

  Elements lista;

  document = Jsoup.connect(site + letra).get();

  lista = document.select(".lin-area-c2");

  for (Element elemento: lista) {
    cursos.add(elemento.text());
  }

  return cursos;
}
  • I did as you were saying but as I was only giving the error android.os.Networkonmainthreadexception I had to put a thread , I did Log. d(tag,buscarCursos(letraPicker.getSelectedItem.toString())) and gave me an empty Arraylist Return

  • I thread.start

  • Gave upVote for your time , if you could help me with the information I gave you would be very grateful

  • I’ve made it work thanks

  • @Emanuelsobreiro tell us how you solved

  • I’ve done it but I have a problem , when I select the letter it doesn’t upgrade , it only does when I click this letter twice in a row

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.