There’s no other error, except that you should return an empty list instead of null. App Inventor doesn’t respond well to null, although they should definitely add that.
Jsoup does not parse html synchronously so you need to check whether document is null or not on a regular interval.
This thing is missing in HtmlUtils extension too.
package com.ruwis.webslapper;
import java.util.List;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.util.YailList;
import com.google.appinventor.components.runtime.util.AsynchUtil;
public class WebSlapper extends AndroidNonvisibleComponent {
public WebSlapper(ComponentContainer container) {
super(container.$form());
}
// Función modificada para usar un hilo separado
@SimpleFunction
public void VeriCek(final String url, final String css) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
try {
Connection connect = Jsoup.connect(url).userAgent("Mozilla/5.0");
Document doc = connect.get();
Elements elements = doc.select(css);
// Si no se encontraron elementos, devuelve explícitamente null
if (elements.isEmpty()) {
returnResult(null); // Retorna null explícitamente
} else {
returnResult(YailList.makeList(elements.eachText())); // Convierte a lista de App Inventor
}
} catch (Exception e) {
returnResult(null); // En caso de error, retorna null
}
}
});
}
// Método para retornar el resultado a App Inventor en el hilo principal
private void returnResult(final List<String> result) {
form.runOnUiThread(new Runnable() {
@Override
public void run() {
if (result == null) {
// Enviar null a App Inventor
WebSlapped(null);
} else {
// Enviar la lista obtenida a App Inventor
WebSlapped(result);
}
}
});
}
// Evento para devolver el resultado a App Inventor
@SimpleFunction
public void WebSlapped(List<String> result) {
// Implementa este evento para manejar los resultados en App Inventor
}
// Método para manejar errores
@SimpleFunction
public void ErrorOccurred(String errorMessage) {
// Implementa el manejo de errores
}
}