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
Yeah, right, it should always do it is not the main thread.
You can do it without an event.
There are magic Java classes that can do (asynchronously) without using the Deprecated AsyncTask of Android, this is a bit complicated to understand (at first) so it's not advisable to use this.
Do you know about while loop and Threed.sleep() method?
I am not using any kind of while loop or for a loop.