Creating Extensions with AsyncTask (android.os.AsyncTask) possible?

107 views
Skip to first unread message

Arne Mailand

unread,
Oct 2, 2016, 2:46:19 PM10/2/16
to App Inventor Open Source Development
Well is this possible or do we have to use "real" Threads?

If it is possible, how should this be done?

kind regards

Arne Mailand

unread,
Oct 2, 2016, 2:58:24 PM10/2/16
to App Inventor Open Source Development
Well, I made this:

package com.google.appinventor.components.runtime;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
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.annotations.UsesPermissions;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.util.AsynchUtil;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.FileUtil;
import com.google.appinventor.components.runtime.Form;
import com.google.appinventor.components.runtime.ReplForm;

import android.os.AsyncTask;
import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;

@DesignerComponent(version = YaVersion.FILE_COMPONENT_VERSION,
        description = "Meine erste Extension",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "images/extension.png")
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.WRITE_EXTERNAL_STORAGE, android.permission.INTERNET, android.permission.READ_EXTERNAL_STORAGE")
public class Filet extends AndroidNonvisibleComponent implements Component{

    public Filet(ComponentContainer container){
        super(container.$form());
    }

    @SimpleFunction(description = "Funktionsbeschreibung hier")
    public void Filet2(){

        new Connect().execute();
    }


    public class void Connect extends AsyncTask<Void, Void, Void>{
        protected Void doInBackground() {
        Socket socket;

        File file = new File(Environment.getExternalStorageDirectory(), "bild.jpg");
        byte[] bytes = new byte[(int) file.length()];

        try {
            socket = new Socket("192.168.178.32", 80);

            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

            bis.read(bytes, 0, bytes.length);

            bos.write(bytes, 0, bytes.length);
            bos.flush();
            socket.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            bis.close();
//            bos.close();
        }
        }

    }
}

Compiler is telling me:

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:60: error: <identifier> expected

    [javac]     public class void Connect extends AsyncTask<Void, Void, Void>{

    [javac]                 ^

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:60: error: <identifier> expected

    [javac]     public class void Connect extends AsyncTask<Void, Void, Void>{

    [javac]                              ^

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:60: error: <identifier> expected

    [javac]     public class void Connect extends AsyncTask<Void, Void, Void>{

    [javac]                                                                  ^

    [javac] 3 errors


When I put the inner class public class void Connect extends AsyncTask<Void, Void, Void>{} to outside, I even get more errors. So I assume, it wont work?

Evan Patton

unread,
Oct 2, 2016, 3:33:59 PM10/2/16
to App Inventor Open Source Development
The reason you are getting errors is because that is not a valid class definition. The type is class, not void, so it should be public class Connect extends AsyncTask<Void, Void, Void>.

Evan

Jos Flores

unread,
Oct 2, 2016, 3:46:47 PM10/2/16
to app-inventor-open-source-dev
You should be able to use AsyncUtil as explained in the docs here:
https://docs.google.com/document/d/1xk9dMfczvjbbwD-wMsr-ffqkTlE3ga0ocCE1KOb2wvw/pub#h.8te56x1w9u8b

Using AsyncTask directly is fine, but the abstraction above seems to
be easier to use.

cheers,
José
> --
> 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 post to this group, send email to
> app-inventor-o...@googlegroups.com.
> Visit this group at
> https://groups.google.com/group/app-inventor-open-source-dev.
> For more options, visit https://groups.google.com/d/optout.

Arne Mailand

unread,
Oct 2, 2016, 6:26:20 PM10/2/16
to App Inventor Open Source Development
Thanks Evan, I am so dumb! I removed that void and now that line seems to be fine, but now I get this error:

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:84: error: missing return statement

    [javac] }

    [javac] ^


This error is referring to one of the last lines of my code. This sounds like a noob mistake like I had many times before, but I just cant fix it. The numbers of opening and closing backets should be right. If I insert return null; here, I get the errors


    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:85: error: illegal start of type

    [javac] return null;}

    [javac] ^

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:85: error: ';' expected

    [javac] return null;}

    [javac]       ^

    [javac] 2 errors


Any ideas how to fix this?

Arne Mailand

unread,
Oct 2, 2016, 6:28:44 PM10/2/16
to App Inventor Open Source Development
Btw when I remove null then I still get

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:85: error: illegal start of type

    [javac] return;}

    [javac] ^

    [javac] 1 error

Arne Mailand

unread,
Oct 2, 2016, 6:32:32 PM10/2/16
to App Inventor Open Source Development
Yes that's the "real" threads I meant. AsyncTask would be easier since I get a code with it already, but if I dont get it working, I will switch to that option. Thanks

Arne Mailand

unread,
Oct 2, 2016, 7:32:49 PM10/2/16
to App Inventor Open Source Development
Well, I guess the cause of the "missing return statement" error is in this line:

        protected Void doInBackground(Void... void){

Originally, I had no parameters set, which caused errors related with overriding. With the first Void written in big letter, I get a missing-return-statement error. What I now have is this:

        protected void doInBackground(Void...){

causing only this 1 error (all other modifications will cause at least 2 errors, so I assume this should be the closest to solution?):

    [javac] /Users/kirikiri3/Documents/mai/appinventor/components/src/com/google/appinventor/components/runtime/Filet.java:63: error: <identifier> expected

    [javac]         protected void doInBackground(Void...){

    [javac]                                              ^


Reply all
Reply to author
Forward
0 new messages