import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.ComponentConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
//import org.json.simple.JSONObject;
@DesignerComponent(version = YaVersion.WEB_COMPONENT_VERSION,
description = "Get the url and return response content",
category = ComponentCategory.EXTENSION,
nonVisible=true,
iconName = "images/recupera.png"
)
@SimpleObject(external=true)
public class RetornaConteudoSite extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 4;
private ComponentContainer container;
private Context context;
private static final String LOG_TAG = "RetornaConteudoSite";
public RetornaConteudoSite(ComponentContainer container){
super(container.$form());
this.container = container;
context = (Context) container.$context();
Log.d(LOG_TAG, "RetornaConteudoSite Created" );
}
//@SimpleFunction
public static void main(String[] args) {
//System.out.println("Seu texto é inserido aqui, entre aspas duplas");
//String URL = null;
GetValue(URL);
//System.out.print(link);
}
//@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING, defaultValue = "")
//@SimpleProperty
@SimpleFunction
public static String GetValue(String URL) {
String x = null;
try {
x = RetornaConteudoSite.call_me(URL);
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(t);
return x;
}
//@DesignerProperty(editorType = PropertyTypeConstants.)
//@SimpleFunction(description = "Retorna o texto JSON")
public static String call_me(String URLFB) throws Exception {
URL obj = new URL(URLFB);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
//System.out.println("\nSending 'GET' request to URL : " + URLFB);
//System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print in String
//System.out.println(response.toString());
String link_final = response.toString();
return link_final;
}
}