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
Btw
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
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...
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;
}
Leave I am going to use event