package com.gordonlu.mailcheck; 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.io.ByteArrayOutputStream; import com.google.appinventor.components.runtime.util.AsynchUtil; import java.io.BufferedReader; import java.lang.StringBuilder; import java.io.InputStreamReader; import java.net.URL; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @DesignerComponent(version = 2, category = ComponentCategory.EXTENSION, description = "A test extension. Created by Gordon.", nonVisible = true, iconName = "images/extension.png") @SimpleObject(external = true) public class MailCheck extends AndroidNonvisibleComponent { public MailCheck(ComponentContainer container){ super(container.$form()); } @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(); } }); } } }); } @SimpleEvent(description = "Test event.") public void TestEvent(String response) { EventDispatcher.dispatchEvent(this, "TestEvent", response); } }