Okay
Do you want me to give the extension first so you can see how it works or the source code
Yes, very very sure.....
extension?
or source code?
Source code is needed. You don't waste time compiling extension...
Okay, please try it, I have tried it and my code was successful 100%
com.extension.issinfo.aix (6.1 KB)
IssInfo.txt (2.2 KB)
But what are the imports
Please wait, can I check
Imports needed:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
You should do network operations ( such as getting information from an API ) on another thread, or a NetworkOnMainThread
exception will be thrown when your code runs, since blocks runs on the Main thread, see the example codes above to know how to run your code on a new thread, with the AsynchUtil
class for example.
Replace method code:
@SimpleFunction()
public void GetJSONText() {
new Thread(new Runnable() {
@Override
public void run() {
String url = "https://api.wheretheiss.at/v1/satellites/25544";
try {
URL u = new URL(url);
HttpURLConnection con = (HttpURLConnection) u.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
br.close();
postText(sb.toString());
} catch (Exception ex) {
ex.printStackTrace();
postText(ex.getMessage() != null?ex.getMessage():ex.toString());
}
}
}).run();
}
public void postText(final String text){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotText(text);
}
});
}
@SimpleEvent()
public void GotText(String text){
EventDispatcher.dispatchEvent(this,"GotText",text);
}
Imports:
import android.app.Activity;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.*;
import com.google.appinventor.components.runtime.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
Declare Activity in class:
public Activity activity;
Initialize in container:
activity = container.$context();
check now @Alpha2020 @Salman_Dev
Can you make that a void.
you mean, you want me to change it to public void?
Wow, that's really cool @vknow360
Okay @Alpha2020
This is the version I made and I updated it with a code that vknow360 fixed
IssInfo.txt (2.1 KB)
And This is the version that uses public void by @vknow360
IssInfo.txt (2.3 KB)
So, if your problem has been resolved, please close this topic by pressing the Solution button, so other people who want to learn how to retrieve json from link with the code, can learn from the topic you created. Thank you