My app inventor extension doesn't get the variable value!

58 views
Skip to first unread message

base dados

unread,
Aug 2, 2019, 10:22:29 AM8/2/19
to App Inventor Open Source Development
Hi! I'm trying to develop an extension for MIT app inventor that gets a link and returns the response content. But when i insert the link in my block, app inventor shows the following error: "The operation Text cannot accept the arguments: ,[*nothing*]". Someone could help me?

The image below shows what i'm trying to do: 

blocks.png

And the code below shows my code in java: 

package retornaconteudosite;

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;
        String URL = "https://www.google.com.br/";
        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;
       }
    }




Abraham Getzler

unread,
Aug 2, 2019, 10:29:25 AM8/2/19
to App Inventor Open Source Development
I'm not an internals expert, but I notice
you are trying to give an immediate response for
an input that usually requires an Event block.

Have you seen any examples of other extensions
that do that?

ABG

base dados

unread,
Aug 2, 2019, 10:38:35 AM8/2/19
to App Inventor Open Source Development
When i run this code in NetBeans, it gives me the response content of any link. But in app inventor it's like no parameter was passed to the function.

Jeffrey Schiller

unread,
Aug 2, 2019, 12:08:09 PM8/2/19
to app-inventor-o...@googlegroups.com
You should look at the "logcat" output on your device when you run your code. There may be log entries there that give you a clue what is going on. I suspect that the problem you are running into is that modern versions of Android to *not* allow you to do Network I/O on the main UI thread. Instead you have to create a handler and "post" the network I/O as a "Runnable". This means that your code will run asynchronously, so you will not get the answer right away. Instead you need to define an event handler to fire when the result is available from the asynchronous handler.

-Jeff

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-so...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/app-inventor-open-source-dev/a1dd1e94-3e35-41ab-a389-e12083272b3a%40googlegroups.com.


--
_______________________________________________________________________
Jeffrey I. Schiller
Information Systems and Technology – MIT App Inventor
Massachusetts Institute of Technology
77 Massachusetts Avenue  Room 32G-530
Cambridge, MA 02139-4307
617.910.0259 - Voice
_______________________________________________________________________

base dados

unread,
Aug 6, 2019, 9:50:09 AM8/6/19
to App Inventor Open Source Development
Hi, Jeff! I'm creating an app that uploads multiple pictures and i need this block to get the response content of each photo. If i run asynchronously, my code won't be able to retrieve more than one response content automacally. My problem is: i don't know how to make a block that receives a link and returns the response content. The code in java works well, but i don't know how to make this work in Mit App Inventor.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-source-dev+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages