A mix of both actually. Only the variables are in Kotlin.
This should be your code…
package domainExtension.domainName.extensionNameInLowercase;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
// For App Inventor & it's clones đź‘˝
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.*;
import com.google.appinventor.components.runtime.*;
// Context isn't for App Inventor or it's clones, but where a context is needed, this will help
import android.content.Context;
@DesignerComponent(
version = 1, // Increment with updates
description = "Describe what your extension does",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "images/extension.png",
helpUrl = "https://google.com or your URL")
@SimpleObject(external = true)
public class ExtensionFileName extends AndroidNonvisibleComponent {
private ComponentContainer container;
// Uses android.content.Context
private Context context;
public ExtensionFileName(ComponentContainer container) {
super(container.$form());
this.container = container;
// Sets the application context, accessible from the extension
this.context = container.$context();
}
// This is a yellow event block
@SimpleEvent(description = "What does this block do?")
public void OnRequestResponse(String body, int statusCode) {
EventDispatcher.dispatchEvent(this, "OnRequestResponse", body, statusCode);
}
// This is a purple block that would be used inside of yellow blocks
@SimpleFunction(description = "What does this block do?")
public void CreateRequest(String url) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
HttpClient client = HttpClient.newBuilder().build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
OnRequestResponse(response.body(), response.statusCode());
}
}
Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml
extensions:
clean:
init:
common_CommonUtils:
init:
CommonUtils:
common_CommonVersion:
init:
CommonVersion:
[exec] Result: 128
[exec] Result: 128
CopyToRunLibDir:
components_AndroidRuntime:
init:
CommonConstants:
[javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[javac] Creating empty /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants/com/google/appinventor/components/common/package-info.class
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants.jar
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants-gwt.jar
HtmlEntities:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] Compiling 1 source file to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] 1 warning
[jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/components/build/HtmlEntities.jar
common_CommonVersion:
init:
CommonVersion:
[exec] Result: 128
[exec] Result: 128
AndroidRuntime:
[mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] Compiling 332 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:7: error: package java.net.http does not exist
[javac] import java.net.http.HttpClient;
[javac] ^
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:8: error: package java.net.http does not exist
[javac] import java.net.http.HttpRequest;
[javac] ^
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:9: error: package java.net.http does not exist
[javac] import java.net.http.HttpResponse;
[javac] ^
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:49: error: cannot find symbol
[javac] HttpRequest request = HttpRequest.newBuilder [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:49: error: cannot find symbol
[javac] HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
[javac] ^
[javac] symbol: class HttpRequest
[javac] location: class ExtensionFileName
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:49: error: cannot find symbol
[javac] HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(url)).build();
[javac] ^
[javac] symbol: variable HttpRequest
[javac] location: class ExtensionFileName
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:50: error: cannot find symbol
[javac] HttpClient client = HttpClient.newBuilder().build();
[javac] ^
[javac] symbol: class HttpClient
[javac] location: class ExtensionFileName
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:50: error: cannot find symbol
[javac] HttpClient client = HttpClient.newBuilder().build();
[javac] ^
[javac] symbol: variable HttpClient
[javac] location: class ExtensionFileName
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/ExtensionFileName.java:51: error: cannot find symbol
[javac] HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
[javac] ^
[javac] symbol: class HttpResponse
[javac] location: class ExtensionFileName
[javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/baalveer78692/ExtensionFileName/Extensio [javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with [javac] Note: Some input files use or override a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 9 errors
[javac] 1 warning
BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.
Total time: 7 seconds
Yes but @hammerhai has used it in code which you are trying to compile.
If you can wait I’ll give you code tomorrow.
Or easiest solution is to perform network request in AsynchUtil thread and dispatch response using runOnUiThread.