Fetching from API. Please Help me

But what are the imports :thinking:

Please wait, can I check

1 Like

Imports needed:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
2 Likes

image

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.

2 Likes

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

2 Likes

Can you make that a void.

Did you just update this @vknow360 :sweat_smile:

you mean, you want me to change it to public void?

1 Like

Wow, that's really cool @vknow360 :heart_eyes:


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 :grin: :kissing_heart:

2 Likes

I just fixed your code by adding @UsesPermissions
IssInfo.txt (2.3 KB)

1 Like

Thank you for adding @UsesPermissions @Nisarga_Adhikary
but I don't think it really matters :sweat_smile:

3 Likes

@Salman_Dev, If possible can you make an aia file and send me. :sweat_smile: :grin:

1 Like

yeah, I can make it for you, but which code do you want to use, from vknow360 or me?

1 Like

Thank you, I needed the latest code where everything is added. :joy:

1 Like

okay let's use the code from vknow360

1 Like

Aia Project :
IssInfo.aia (8.3 KB)
Aix File :
com.extension.issinfo.aix (7.6 KB)

1 Like

It is showing this-

android.os.NetworkOnMainThreadException
2 Likes

Do request on another thread using asynchutil