Need help creating an extension

https://viacep.com.br/ws/01001000/json

**URL => https://viacep.com.br/ws/ + Number 01001000 + /json **

Cep.aia (7.6 KB)

Can someone help me I'm having trouble creating this extension, I wish this was the result :point_down: could someone teach me the introduction?

component_event (2)

component_method (7)

Why do you need an extension?

Simply join the url, the number, and the /json using text blocks

you can use web component instead just use the join block in text as @TIMAI2 said .
or you want to learn how to create an Extension.

@TIMAI2 could you show me how I get this result is giving error

cep.txt (1.8 KB)

   @SimpleFunction(description = "Website source-code")
    public void GetCEP() {
        AsynchUtil.runAsynchronously(new Runnable() { // Always do get request Asynchronously
            @Override
            public void run() {
                try {
                    BufferedReader readStream = new BufferedReader(
                            new InputStreamReader(
                                    new URL("https://viacep.com.br/ws/01001000/json").openStream())); // Open stream and read the content from streamReader to BufferedReader
                    String readLine; // Variable which will have one line of data
                    StringBuilder data = new StringBuilder(); // The result data will be stored here
                    while ((readLine = readStream.readLine()) != null) data.append(readLine); // Read all the lines from the readStream
                    readStream.close(); // IMPORTANT close the stream

                    final String finalData = data.toString(); // Make the string final with the data variable
                    activity.runOnUiThread(new Runnable() { // Always raise events on UI thread
                        @Override
                        public void run() {
                            GotCEP(finalData); // You're event with data
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace(); // Error occured
                }
            }
        });

    } 
    
    @SimpleEvent(description = "voltou")
    public void GotCEP (String cep){
        EventDispatcher.dispatchEvent(this, "GotCEP", cep);
    }

}

Java code using these blocks, can teach me how to make these blocks below, like java code

if you want this as an Extension here is it Enjoy


Source code:

package com.mrkoder.cep.dev;

import android.app.Activity;
import android.content.Context;
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 org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Handler;

@DesignerComponent(
        version = 1,
        description = "",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "")

@SimpleObject(external = true)
@UsesLibraries(libraries = "")
@UsesPermissions(permissionNames = "")

public class CEP extends AndroidNonvisibleComponent {
    private Context context;
    private Activity activity;

    public CEP(ComponentContainer container) {
        super(container.$form());
        this.activity = container.$context();
        this.context = container.$context();
    }

    @SimpleFunction(description = "Fetch CEP Data")
    public void FetchCEPData(final String cepNumber) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String apiUrl = "https://viacep.com.br/ws/" + cepNumber + "/json";
                    URL url = new URL(apiUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);

                    int responseCode = connection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        InputStream inputStream = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                        StringBuilder response = new StringBuilder();
                        String line;
                        while ((line = reader.readLine()) != null) {
                            response.append(line);
                        }
                        reader.close();
                        inputStream.close();

                        JSONObject jsonResponse = new JSONObject(response.toString());
                        final String cep = jsonResponse.getString("cep");
                        final String logradouro = jsonResponse.getString("logradouro");
                        final String complemento = jsonResponse.getString("complemento");
                        final String bairro = jsonResponse.getString("bairro");
                        final String localidade = jsonResponse.getString("localidade");
                        final String uf = jsonResponse.getString("uf");
                        final String ibge = jsonResponse.getString("ibge");
                        final String gia = jsonResponse.getString("gia");
                        final String ddd = jsonResponse.getString("ddd");
                        final String siafi = jsonResponse.getString("siafi");
                        
                        // Pass the full response as a parameter
                        final String fullResponse = response.toString();

                        Handler handler = new Handler(context.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                AddressInfoResult(cep, logradouro, complemento, bairro, localidade, uf, ibge, gia, ddd, siafi, fullResponse);
                            }
                        });
                    } else {
                        Handler handler = new Handler(context.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                AddressInfoResult("Error", "API request failed", "", "", "", "", "", "", "", "", "");
                            }
                        });
                    }
                } catch (final Exception e) { // Declare e as final here
                    Handler handler = new Handler(context.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            AddressInfoResult("Error", e.getMessage(), "", "", "", "", "", "", "", "", "");
                        }
                    });
                }
            }
        }).start();
    }

    @SimpleEvent(description = "Event triggered with address information.")
    public void AddressInfoResult(
            String cep,
            String logradouro,
            String complemento,
            String bairro,
            String localidade,
            String uf,
            String ibge,
            String gia,
            String ddd,
            String siafi,
            String fullResponse) {
        EventDispatcher.dispatchEvent(this, "AddressInfoResult", cep, logradouro, complemento, bairro, localidade, uf, ibge, gia, ddd, siafi, fullResponse);
    }
}

Blocks:

Results:


Aix File:

CEP.aix (9.9 KB)


2 Likes

@Black_Knight thank you friend this will help a lot in my studies :heart:

1 Like

@maria1 Not to be offensive, but I suggest doing some research before asking questions, because it would help you in learning to do research. All of your questions were publicly addressed before, so it would be a better idea to search the community/Google instead.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.