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);
}
}