Return code do not work

Return code do not work
Code

import android.app.Activity;

import com.google.appinventor.components.annotations.DesignerComponent;

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.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import com.google.appinventor.components.runtime.util.AsynchUtil;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

@DesignerComponent(version = 1,

       category = ComponentCategory.EXTENSION,

       description = "Made my YOU",

       nonVisible = true,

       iconName = "ICON LINK")

@SimpleObject(external = true)

public class pereodix_1 extends AndroidNonvisibleComponent {

   private Activity activity;

   public pereodix_1(ComponentContainer container) {

       super(container.$form());

       activity = container.$context();

   }

   @SimpleFunction

   public Object DoRequest(final String op) {

       AsynchUtil.runAsynchronously(new Runnable() {

           @Override

           public void run() {

               BufferedReader in;

               try {

                   in = new BufferedReader(

                           new InputStreamReader(

                                   new URL("my api comes here "+op).openStream()));

                   String inputLine;

                   final StringBuilder result = new StringBuilder();

                   while ((inputLine = in.readLine()) != null)

                       result.append(inputLine);

                   in.close();
              
                   return result;

               } catch (IOException e) {

                   e.printStackTrace();

               }

           }
       });
   }

Error

Started Compiling Project pereodix_1
Buildfile: /compiler/android/build.xml

javac:
[mkdir] Created dir: /compiler/android/build/HVYHE/classes
[javac] Compiling 1 source file to /compiler/android/build/HVYHE/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] warning: In component pereodix_1, method DoRequest is missing a description.
[javac] Note: Wrote file file:/compiler/android/build/HVYHE/classes/simple_components.json
[javac] warning: In component pereodix_1, method DoRequest is missing a description.
[javac] Note: Wrote file file:/compiler/android/build/HVYHE/classes/simple_components.txt
[javac] Note: Wrote file file:/compiler/android/build/HVYHE/classes/simple_components_build_info.json
[javac] warning: In component pereodix_1, method DoRequest is missing a description.
[javac] Note: Wrote file file:/compiler/android/build/HVYHE/classes/AutogeneratedOdeMessages.java
[javac] Note: Wrote file file:/compiler/android/build/HVYHE/classes/ComponentsTranslation.java
[javac] warning: In component pereodix_1, method DoRequest is missing a description.
[javac] /compiler/android/src/HVYHE/com/yash/pereodix/pereodix_1.java:78: error: incompatible types: unexpected return value
[javac] return result;
[javac] ^
[javac] 1 error
[javac] 4 warnings

You can't return anything from void methods

How can you tell me

I can't because, You gets angry when we say Learn Basics :grimacing: :grimacing:
Btw

2 Likes
import android.app.Activity;

import com.google.appinventor.components.annotations.DesignerComponent;

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.common.ComponentCategory;

import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;

import com.google.appinventor.components.runtime.ComponentContainer;

import com.google.appinventor.components.runtime.EventDispatcher;

import com.google.appinventor.components.runtime.util.AsynchUtil;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

@DesignerComponent(version = 1,

        category = ComponentCategory.EXTENSION,

        description = "Made my YOU",

        nonVisible = true,

        iconName = "ICON LINK")

@SimpleObject(external = true)

public class pereodix_1 extends AndroidNonvisibleComponent {

    private Activity activity;

    public pereodix_1(ComponentContainer container) {

        super(container.$form());

        activity = container.$context();

    }

    @SimpleFunction

    public Object DoRequest(final String op) {

                BufferedReader in;

                try {

                    in = new BufferedReader(

                            new InputStreamReader(

                                    new URL("my api comes here "+op).openStream()));

                    String inputLine;

                    final StringBuilder result = new StringBuilder();

                    while ((inputLine = in.readLine()) != null)

                        result.append(inputLine);

                    in.close();
               
                    return result;

                } catch (IOException e) {

                    e.printStackTrace();

                }
    }
```

Is this is correct

May this help you - Fetching from API. Please Help me

No, it will throw NetworkOnMainThreadException

The code in first post was correct, But instead of returning the result create an event, so users can access the result through it

I do that and that work but I want to return like this

That was recommended way but perhaps You can create a variable to store the result and then return that variable in another method.

String myResult = null;

public void DoReq(){
    // your code for http request
    myResult = result;
}

@SimpleFunction
public String ReturnResult(){
    DoReq();
    if (myResult != null){
        return myResult;
    } else {
        return "something";
    }
    
}

Also that do not work

That's why I said to use an event

Is there no any way to return like this

A piece of code -

AsynchUtil.runAsynchronously(new Runnable() {
            @Override
            public void run() {
                try {
                    URL u = new URL("YOUR API" + op);
                    HttpURLConnection con = (HttpURLConnection) u.openConnection();
                    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line).append("\n");
                    }
                    br.close();
                    postText(sb.toString());
                } catch (Exception ex) {
                    ex.printStackTrace();
                    postText(ex.getMessage() != null?ex.getMessage():ex.toString());
                }
            }
        });
    }
    public void postText(final String text){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                GotResult(text);
            }
        });
    }

Try it...

1 Like

I have attatched sample code here. but still I recommend to use event

That's not the matter. His question is different.

use this line outside your function
return result;

Unfortunately you can't access it outside the run method

i'm sorry my mistake, i mean :

@SimpleFunction
public String ReturnResult(String result1, String result2){
    String dataResult;
    if (result1.isEmpty()){
        dataResult ="code1";
    } else {
        dataResult ="code2";
    }
    return dataResult;
}