Fetching from API. Please Help me

Okay :grin:

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

Hi @Alpha2020
You should check this:

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)

1 Like

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