Android java AsyncTask wait onPostExecute return method
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I wrote the following code, I'm looking for a way to make an http call, process the data and let me return the result.
The problem being an async operation, when I do html.getTi()
, gives me no value because the operation is still running and I'm faster.
I tried to use: synchronized (this) {..
of java but it does not seem to work.
I thought about using a wait loop:
while(html.getFlg ()! = false);
but it takes too many resources and hangs.
I was thinking of using callbacks
or promises
, but I do not know if it's the right way.
Some advice?
Main.java
Html html = new Html().execute("...");
html.getTi();//return element ArrayList<ListItem>
Html.java
public class Html extends Activity {
ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
public Html execute(String str) {
new Http().execute(str);
return this;
}
public ArrayList<ListItem> getTi() {
return listItemList;
}
public class Http extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected Void doInBackground(String... params) {
...
}
@Override
protected void onPostExecute(Void result) {
...
}
}
}
java android android-asynctask callback async-await
add a comment |
I wrote the following code, I'm looking for a way to make an http call, process the data and let me return the result.
The problem being an async operation, when I do html.getTi()
, gives me no value because the operation is still running and I'm faster.
I tried to use: synchronized (this) {..
of java but it does not seem to work.
I thought about using a wait loop:
while(html.getFlg ()! = false);
but it takes too many resources and hangs.
I was thinking of using callbacks
or promises
, but I do not know if it's the right way.
Some advice?
Main.java
Html html = new Html().execute("...");
html.getTi();//return element ArrayList<ListItem>
Html.java
public class Html extends Activity {
ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
public Html execute(String str) {
new Http().execute(str);
return this;
}
public ArrayList<ListItem> getTi() {
return listItemList;
}
public class Http extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected Void doInBackground(String... params) {
...
}
@Override
protected void onPostExecute(Void result) {
...
}
}
}
java android android-asynctask callback async-await
You have tons of ways of doing it. For me, the simplest one is just to add a listener reference to call after you finish the task.
– haroldolivieri
Nov 16 '18 at 14:08
add a comment |
I wrote the following code, I'm looking for a way to make an http call, process the data and let me return the result.
The problem being an async operation, when I do html.getTi()
, gives me no value because the operation is still running and I'm faster.
I tried to use: synchronized (this) {..
of java but it does not seem to work.
I thought about using a wait loop:
while(html.getFlg ()! = false);
but it takes too many resources and hangs.
I was thinking of using callbacks
or promises
, but I do not know if it's the right way.
Some advice?
Main.java
Html html = new Html().execute("...");
html.getTi();//return element ArrayList<ListItem>
Html.java
public class Html extends Activity {
ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
public Html execute(String str) {
new Http().execute(str);
return this;
}
public ArrayList<ListItem> getTi() {
return listItemList;
}
public class Http extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected Void doInBackground(String... params) {
...
}
@Override
protected void onPostExecute(Void result) {
...
}
}
}
java android android-asynctask callback async-await
I wrote the following code, I'm looking for a way to make an http call, process the data and let me return the result.
The problem being an async operation, when I do html.getTi()
, gives me no value because the operation is still running and I'm faster.
I tried to use: synchronized (this) {..
of java but it does not seem to work.
I thought about using a wait loop:
while(html.getFlg ()! = false);
but it takes too many resources and hangs.
I was thinking of using callbacks
or promises
, but I do not know if it's the right way.
Some advice?
Main.java
Html html = new Html().execute("...");
html.getTi();//return element ArrayList<ListItem>
Html.java
public class Html extends Activity {
ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
public Html execute(String str) {
new Http().execute(str);
return this;
}
public ArrayList<ListItem> getTi() {
return listItemList;
}
public class Http extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() { super.onPreExecute(); }
@Override
protected Void doInBackground(String... params) {
...
}
@Override
protected void onPostExecute(Void result) {
...
}
}
}
java android android-asynctask callback async-await
java android android-asynctask callback async-await
asked Nov 16 '18 at 14:02
PaulPaul
154210
154210
You have tons of ways of doing it. For me, the simplest one is just to add a listener reference to call after you finish the task.
– haroldolivieri
Nov 16 '18 at 14:08
add a comment |
You have tons of ways of doing it. For me, the simplest one is just to add a listener reference to call after you finish the task.
– haroldolivieri
Nov 16 '18 at 14:08
You have tons of ways of doing it. For me, the simplest one is just to add a listener reference to call after you finish the task.
– haroldolivieri
Nov 16 '18 at 14:08
You have tons of ways of doing it. For me, the simplest one is just to add a listener reference to call after you finish the task.
– haroldolivieri
Nov 16 '18 at 14:08
add a comment |
1 Answer
1
active
oldest
votes
You can simply use library okHttp
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(YOUR_URL)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the result
}
}
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339364%2fandroid-java-asynctask-wait-onpostexecute-return-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can simply use library okHttp
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(YOUR_URL)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the result
}
}
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
add a comment |
You can simply use library okHttp
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(YOUR_URL)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the result
}
}
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
add a comment |
You can simply use library okHttp
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(YOUR_URL)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the result
}
}
You can simply use library okHttp
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(YOUR_URL)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
} else {
// do something wih the result
}
}
answered Nov 16 '18 at 14:21
OnixOnix
4579
4579
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
add a comment |
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
I'm trying to see if it works, but if I wanted to set it as a method and pass a callback or a promise as an argument, how can I do?
– Paul
Nov 16 '18 at 16:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339364%2fandroid-java-asynctask-wait-onpostexecute-return-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have tons of ways of doing it. For me, the simplest one is just to add a listener reference to call after you finish the task.
– haroldolivieri
Nov 16 '18 at 14:08