Error while testing my extension

hello there I am here today cause I need help creating an extension with Niotron IDE
my problem is :
that when I complete my extension code and while testing it gives me an error message "undefined" from the companion
this extension about checking email validation by using API
this is the code :slight_smile:

code
package com.koder.email.check.disp.valid.dev;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.json.JSONObject;

@DesignerComponent(
        version = 1,
        description = "",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class Email_Chekcer extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;
    private String xRapidAPIKey;
    private boolean valid;
    private boolean disposable;

    public Email_Chekcer(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "Sample Function Generated by Niotron")
    public void TestFunction(String domain, String xRapidAPIKey) throws IOException {
        this.xRapidAPIKey = xRapidAPIKey;
        URL url = new URL("https://mailcheck.p.rapidapi.com/?domain=" + domain);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("X-RapidAPI-Host", "mailcheck.p.rapidapi.com");
        connection.setRequestProperty("X-RapidAPI-Key", xRapidAPIKey);

        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        //Getting the response
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //Parsing the response
        JSONObject jsonResponse = new JSONObject(response.toString());
        valid = jsonResponse.getBoolean("valid");
        disposable = jsonResponse.getBoolean("disposable");
        EventDispatcher.dispatchEvent(this, "TestEvent", valid, disposable);
    }

    @SimpleProperty(description = "Valid value from API")
    public boolean isValid() {
        return valid;
    }

       @SimpleProperty(description = "Disposable value from API")
    public boolean isDisposable() {
        return disposable;
    }

    @SimpleEvent(description = "Test Event Generated by Niotron")
    public void TestEvent(boolean valid, boolean disposable) {
        EventDispatcher.dispatchEvent(this, "TestEvent", valid, disposable);
    }
}

Try removing this line

Not working still giving same error
Screenshot 2023-01-12 214638

Try this ( I do not have a domain or rapid api key to test with)

uk.co.metricrat.emailchecker.aix (6.4 KB)

(re-created in RUSH using IntelliJ IDE to clean up syntax/bugs)

unfortunately, the same error exists.
this is API key you can test it: 43ae771ac0msh7c7a2c1131746d6p14aaa8jsn0db2e7e754e9
and replace the domain with virtual or real email

It's possible that there's a stack trace associated with the error. Do you see anything in adb logcat?

This works OK

ADB logcat output from running extension

logcatadb.txt (34.0 KB)

Have stripped back the function but still get undefined

@SimpleFunction(description = "Sample Function Generated by RUSH")
  public void TestFunction(String domain, String xRapidAPIKey) throws IOException {
    try {
    URL url = new URL("https://mailcheck.p.rapidapi.com/?domain=" + domain);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("X-RapidAPI-Host", "mailcheck.p.rapidapi.com");
    connection.setRequestProperty("X-RapidAPI-Key", xRapidAPIKey);
    connection.setRequestMethod("GET");

    //Getting the response
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    StringBuilder response = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
      response.append(inputLine);
    }
    in.close();

    TestEvent(response.toString());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

Sir, you must run these events asynchronously. Wrap your contents in your function with AsynchUtil.runAsynchronously. Take a look at my source code.

2 Likes

ok thanks

yes, I will try it.

Try if this works.

    @SimpleFunction(description = "Sample Function Generated by RUSH")
    public void TestFunction(final String domain, final String xRapidAPIKey) throws IOException {
        AsynchUtil.runAsynchronously(new Runnable () {
            @Override
            public void run() {
            try {
                URL url = new URL("https://mailcheck.p.rapidapi.com/?domain=" + domain);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestProperty("X-RapidAPI-Host", "mailcheck.p.rapidapi.com");
                connection.setRequestProperty("X-RapidAPI-Key", xRapidAPIKey);
                connection.setRequestMethod("GET");
            
                //Getting the response
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                final StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                  response.append(inputLine);
                }
                in.close();
                form.runOnUiThread(new Runnable () {
                    @Override
                    public void run() {
                    TestEvent(response.toString());
                    }
                });
                } catch (final IOException e) {
                    form.runOnUiThread(new Runnable () {
                        @Override
                        public void run() {
                            e.printStackTrace();
                        }
                    });
                }
        }
        });
    }

MailCheck.txt (2.9 KB)

1 Like

I recreated the code

code
package com.koder.email.check.disp.valid.dev;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import com.google.appinventor.components.runtime.util.AsynchUtil;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

@DesignerComponent(
        version = 1,
        description = "",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class Email_Checker extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;
    private String xRapidAPIKey;
    private boolean valid;
    private boolean disposable;

    public Email_Checker(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "Checks the validity and disposable status of the provided email address")
    public class Email_Checker extends AndroidNonvisibleComponent {

    //Activity and Context
    private Context context;
    private Activity activity;
    private String xRapidAPIKey;
    private boolean valid;
    private boolean disposable;

    public Email_Checker(ComponentContainer container){
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }
    
    @SimpleFunction(description = "Checks the validity and disposable status of the provided email address")
    public void CheckEmail(final String email, final String xRapidAPIKey) throws IOException {
        AsynchUtil.runAsynchronously(new Runnable () {
            @Override
            public void run() {
                try {
                    String encodedEmail = URLEncoder.encode(email, "UTF-8");
                    URL url = new URL("https://mailcheck.p.rapidapi.com/check?email=" + encodedEmail);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestProperty("X-RapidAPI-Host", "mailcheck.p.rapidapi.com");
                    connection.setRequestProperty("X-RapidAPI-Key", xRapidAPIKey);
                    connection.setRequestMethod("GET");
                
                    //Getting the response
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    final StringBuilder response = new StringBuilder();
                    while ((inputLine = in.readLine()) != null) {
                      response.append(inputLine);
                    }
                    in.close();
                    JSONObject jsonResponse = new JSONObject(response.toString());
                    valid = jsonResponse.getBoolean("valid");
                    disposable = jsonResponse.getBoolean("disposable");
                    activity.runOnUiThread(new Runnable () {
                        @Override
                        public void run() {
                            EmailChecked(valid, disposable);
                        }
                    });
                } catch (final Exception e) {
                    activity.runOnUiThread(new Runnable () {
                        @Override
                        public void run() {
                            e.printStackTrace();
                        }
                    });
                }
            }
        });
    }
    
    @SimpleEvent(description = "Event triggered when email is checked")
    public void EmailChecked(boolean valid, boolean disposable) {
        EventDispatcher.dispatchEvent(this, "EmailChecked", valid, disposable);
    }

    @SimpleProperty(description = "Valid value from API")
    public boolean isValid() {
        return valid;
    }

    @SimpleProperty(description = "Disposable value from API")
    public boolean isDisposable() {
        return disposable;
    }

}

but it gives me this message after trying to compile

error
Started Compiling Project Email_Chekcer
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/jWpQp/classes
[javac] Compiling 1 source file to /compiler/android/build/jWpQp/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/jWpQp/com/koder/email/check/disp/valid/dev/Email_Chekcer.java:121: error: reached end of file while parsing
[javac] }
[javac] ^
[javac] 1 error
[javac] 1 warning

You are missing } somewhere.

1 Like

this is the class that misses "}" but I tried to put it i the right position it gives me 3 errors

I am a beginner at creating extentions

Thanks @Gordon_Lu for the async code.

This appears to work (built with RUSH)

uk.co.metricrat.emailchecker.aix (8.5 KB)

JAVA
package uk.co.metricrat.emailchecker;

import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.google.appinventor.components.runtime.util.*;
import org.json.*;

public class EmailChecker extends AndroidNonvisibleComponent {

  private String valid;
  private String disposable;

  public EmailChecker(ComponentContainer container){
    super(container.$form());
    Activity activity = container.$context();
    Context context = container.$context();
  }

  @SimpleFunction(description = "Sample Function Generated by RUSH")
  public void TestFunction(final String domain, final String xRapidAPIKey) throws IOException {
    AsynchUtil.runAsynchronously(new Runnable () {
      @Override
      public void run() {
        try {
          URL url = new URL("https://mailcheck.p.rapidapi.com/?domain=" + domain);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setRequestProperty("X-RapidAPI-Host", "mailcheck.p.rapidapi.com");
          connection.setRequestProperty("X-RapidAPI-Key", xRapidAPIKey);
          connection.setRequestMethod("GET");

          //Getting the response
          BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          String inputLine;
          final StringBuilder response = new StringBuilder();
          while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
          }
          in.close();
          form.runOnUiThread(new Runnable () {
            @Override
            public void run() {
              //Parsing the response
              JSONObject jsonResponse = null;
              try {
                jsonResponse = new JSONObject(response.toString());
              } catch (JSONException e) {
                e.printStackTrace();
              }
              try {
                valid = jsonResponse.get("valid").toString();
                disposable = jsonResponse.get("disposable").toString();
                TestEvent(valid, disposable);
              } catch (JSONException e) {
                e.printStackTrace();
              }



              //TestEvent(response.toString());
            }
          });
        } catch (final IOException e) {
          form.runOnUiThread(new Runnable () {
            @Override
            public void run() {
              e.printStackTrace();
            }
          });
        }
      }
    });
  }



  @SimpleProperty(description = "Valid value from API")
  public String isValid() {
    return valid;
  }

  @SimpleProperty(description = "Disposable value from API")
  public String isDisposable() {
    return disposable;
  }

  @SimpleEvent(description = "Test Event Generated by RUSH")
  //public void TestEvent(boolean valid, boolean disposable) {
  //  EventDispatcher.dispatchEvent(this, "TestEvent", valid, disposable);
  //};
  public void TestEvent(String valid, String disposable) {
    EventDispatcher.dispatchEvent(this, "TestEvent", valid,disposable);
  }
}

You will need to add some code back to work in Niotron IDE

1 Like

Consider following the naming conventions as well, otherwise your work may be unlisted. When you are naming your blocks, NEVER use underscores (_).

Email_Checker → EmailChecker

isValid → IsValid

isDisposable → IsDisposable

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.