Search code examples
javaandroidarraysjsoup

Jsoup .data() inside loop


I have a probably quite easy-to-answer question, but I just couldn't find an answer :/

If I connect to a site via jsoup, and want to modify a form in order to submit it by:

upload_doc = Jsoup.connect(url)
                    .cookies(loginCookies)

                    .data(check_radio_yes_name[0], "1") ...

How can I loop the Array inside .data() ? It seems like I can't just insert a for() loop before closing with .get();, so what's the proper way to do this?


Solution

  • According to jsoup's apidoc you can send collections with data, so you can do something like this:

    Map<String, String> myData = new HashMap<String, String>();
    String key = "check_radio_yes_name[";
    for (i=0; i<5; i++)
        myData.put(key + i + "]", "1");  //add your data instead of "1"
    upload_doc = Jsoup.connect(url)
                    .cookies(loginCookies)
                    .data(myData)...
    

    Define all your data in a map, and then add the entire map.