Extension - Returning "null" Help

I am learning develop extensions.

but when i try it’s returning “null”
what is my wrong. Does anyone help me?

package com.ruwis.webslapper;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
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.errors.YailRuntimeError;
import com.google.appinventor.components.runtime.util.YailList;

public class WebSlapper extends AndroidNonvisibleComponent {

  public WebSlapper(ComponentContainer container) {
    super(container.$form());
  }

  
  @SimpleFunction()
  public List<String> VeriCek(String url, String css) {
	  try {
		  Connection connect = Jsoup.connect(url).userAgent("Mozilla/5.0");
		  Document liste = connect.get();
		  Elements secilen = liste.select(css);
		  return YailList.makeList(secilen);
	  }
	  catch(Exception e) {
		  return null;
	  }
  }
  
}

You can’t return null, instead return an empty list.

so do you see any other error?

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.

1 Like

I set it as empty list but it returns empty list every time. I couldn’t find my mistake

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.

2 Likes

hey this post is mine. I solved the problem as in this example.

new Thread() {
		  public void run() {
			  WebSlapper.this.activity.runOnUiThread(new Runnable() {
                  public void run() {
                	  try {
                			Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0").get();
      					Elements elements = doc.select(cssSelector);
      					WebSlapped(elements);
                		}
                		catch(Exception exception) {
                			ErrorOccurred(exception.getMessage());
                		}
                  }
			  
			  
			  });
		  }
		  
	  }.start();

@Peter will one day find out you have two accounts :sweat_smile:

What’s wrong with having 2 accounts?

Only one is allowed on the community. Which one should i delete?

1 Like

Ok npb
You can delete it

It means this one or the other one?

Other account

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
  }
}