Android Studio is insisting that I am missing a return value in doInBackground, even though it seems to be declared as Void. Am I overlooking something?
AsyncTask<Boolean, Void, Void> initiateConnection = new AsyncTask<WebSocketClient, Void, Void>() {
#Override
protected Void doInBackground(WebSocketClient ... clients) {
clients[0].connect();
}
#Override
protected void onPostExecute(Void result) {
Log.i(st, "Socket connected.");
}
};
initiateConnection.execute();
}
Void (uppercase V) is not void (lowercase v). With void, you can just "fall off the bottom of the method". With Void, you need to explicitly return something, typically null.
You need to return null
protected Void doInBackground(WebSocketClient ... clients) {
clients[0].connect();
return null;
}
Related
I would need some help to figure out how to return a doInBackground() value in AsyncTask which code is an interface. Here is an example
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
public MyAsyncTask() {
super();
// do stuff
}
#Override
protected Boolean doInBackground(Void... void) {
checkIfContentAvailable(new interfaceMediaAvailableToDownload() {
#Override
public void onComplete(boolean resutl) {
return result; //This must be doInBackground return value, not onSuccess which is Void
}
#Override
public void onInternetError() {
return false; //This must be doInBackground return value, not onSuccess which is Void
}
};
}
#Override
protected void onPostExecute(Boolean result) {
if (result){
//Do stuff
}else{
//Do stuff
}
}
}
Obviously, this above code can't work because I don't know how to return onSuccess() value to doInBackground().
I hope this is clear enough....
EDIT
Okay my bad, I thought it would have been more readable to hide MyInterface usage, but I realize through your answers it is not. So I completed the code to add more details.
Any idea please?
Thank you in advance.
Create the object of the Mynterface in the place where you execute the AsyncTask.
Create a object reference of the MyInterface inside the AsyncTask ans set your interface object.
Then call the onSuccess method like below
`
private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
MyInteface myInterface;
setMyInterface(MyInterface interface){this.myInterface = interface;}
public MyAsyncTask() {
super();
// do stuff
}
#Override
protected Boolean doInBackground(Void... void) {
this.myInterface.onSuccess(); // or call on failure if failure happened
}
#Override
protected void onPostExecute(Boolean result) {
//Do stuff with result
}
}
`
Use it like ...
MyAsyncTask async = new MyAsyncTask();
async.setMyInterface(this);
async.execute();
Implement the interface in the place where your are executing.
This how you can do it.
Here is small example for to understand my question:
public class InitSettings_Task extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
request1result = request1;
if (request1result) {
result = httprequest2;
} else {
result = httprequest3;
}
return result;
}
#Override
protected void onPostExecute(Integer result) {
//do something with result
}
}
I know that Volley is a super library, but here i can't use it because my AsyncTask can ends before i will receive answer of first request.
Can somebody help me to understand what the best style for to send http request for this logic?
Before i have used Volley with Sleep() for to wait answer, but from my view it's not best sollution
Sounds like you are trying to do this -
public class InitSettings_Task1 extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
request1result = request1;
return result;
}
#Override
protected void onPostExecute(Integer result) {
//do something with result
if (request1result) {
result = new InitSettings_Task2().execute(httprequest2);
} else {
result = new InitSettings_Task2().execute(httprequest3);
}
}
}
public class InitSettings_Task2 extends AsyncTask<Void, Void, Integer> {
#Override
protected Integer doInBackground(Void... params) {
return result;
}
#Override
protected void onPostExecute(Integer result) {
//do what you want with result ?
}
}
But I would advise you against this. Its better to do this using frameworks like RxJava or even EventBus which are better suited for this scenario.
I have realized it with OkHTTP library. Thank's "Selvin" for right direction )
I have an IME service class and a long operation method in it. I want to run the LongOperation task in a asyncTask class that is in the IME Service class.
public class Myimeservice extends InputMethodService
implements KeyboardView.OnKeyboardActionListene {
//...
//some code here....
//...
public void setDictionary(){
//....
}
private class LongOperation extends AsyncTask<String, Void, String> {
private Myimeservice parent;
public LongOperation(Myimeservice pim){
parent = pim;
}
#Override
protected String doInBackground(String... params) {
Myimeservice tmp = new Myimeservice();
tmp.setDictionary();
return "Executed";
}
#Override
protected void onPostExecute(String result) {
//app.hideLoading();
}
#Override
protected void onPreExecute() {
//app.showLoading();
}
#Override
protected void onProgressUpdate(Void... values) {}
}
When i run it, the application forced to close. please help me.
I think the error is somewhere in your public void setDictionary() method.
I assume that you are manipulating a variable that is bound to the UIThread/MainThread, the application will crash since doInBackground is on another Thread.
Instead make the setDictionary() method return the dictionary and return it instead of "Executed" in doInBackground().
This will call the onPostExecute(Object result) which is run on UIThread/MainThread.
Something like this:
private class LongOperation extends AsyncTask<String, Void, Dictionary> {
#Override
protected Dictionary doInBackground(String... params) {
Myimeservice tmp = new Myimeservice();
Dictionary dict = tmp.setDictionary();
return dict;
}
#Override
protected void onPostExecute(Dictionary result) {
//do what ever you meant to do with it;
}
}
If you are not expecting any result from it you can just do:
AsyncTask.execute(new Runnable() {
#Override
public void run() {
tmp.setDictionary();
}
});
I use the Runnable instead of AsyncTask and the problem solved.
final Runnable r = new Runnable(){
public void run(){
setDictionary();
}
};
this code is in onCreate() method of service.
Tanks a lot Tristan Richard.
I know there are several question which are quite similar to this one, but they didn't helped me so far. I'm still not able to override this AsyncTasks.
Can you hel me?
I already made sure that doInBackground() devlivers some type that onPostExecute() requires (Boolean in the first and JSON in the second example).
Do you see anything except that i extend AsyncTask as raw type (was not sure about what to enter here)
This is the error eclipse tells me for each method:
The method onPostExecute(Boolean) of type Register.NetCheck must override or implement a supertype method
and for the classes eclipse tells me:
The type Register.NetCheck must implement the inherited abstract method AsyncTask.doInBackground(Object...)
AsyncTask is a raw type. References to generic type AsyncTask should be
parameterized
EXAMPLE 1:
private class NetCheck extends AsyncTask {
#Override
protected void onPreExecute(){
super.onPreExecute();
do.something();
}
#Override
protected Boolean doInBackground (String... args){
if (do.something.worked){
return true; }
else { return false}
}
#Override
protected void onPostExecute(Boolean th){
evalute.input.boolValue();
}
}
EXAMPLE 2:
private class ProcessRegister extends AsyncTask {
#Override
protected void onPreExecute() {
super.onPreExecute();
show.some.dialog();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONObject json = generate.some.json();
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
evaluate.something();
}
}
Your async task is not expecting a return value, so it does not recognise
protected void onPostExecute(Boolean th)
Try this instead:
private class NetCheck extends AsyncTask<String, Void, Boolean> {
#Override
protected void onPreExecute(){
super.onPreExecute();
do.something();
}
#Override
protected Boolean doInBackground (String... args){
if (do.something.worked){
return true; }
else { return false}
}
#Override
protected void onPostExecute(Boolean th){
evalute.input.boolValue();
}
}
Replace Boolean with JSONObject for the second example. Try to always use the IDE to automatically implement the methods for you, will avoid problems like this.
You should extend AsyncTask with types. See example below.
public class MyTask extends AsyncTask<Void, String, Integer> {
#Override
protected Integer doInBackground(Void... params) {
return null;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
#Override
protected void onCancelled(Integer integer) {
super.onCancelled(integer);
}
#Override
protected void onCancelled() {
super.onCancelled();
}
}
Replaces Void, String, Integer with apropriate types. More info is here.
About Eclipse. Delete in and install Android Studio.
Could anyone tell me what is going wrong here?
public class BackgroungTask extends AsyncTask<String, Void, Void> {
public Void doInBackground(String... params) {
//tasks
return; //error occurs here!
}
public void onPostExecute(Void result) {
//codes
}
}
The above class does not depend on return values. So onPostExceute() simply execute codes written in there.
Thanks in advance!
just return null.
public class BackgroungTask extends AsyncTask<String, Void, Void> {
public Void doInBackground(String... params) {
//tasks
return null;
}
public void onPostExecute(Void result) {
//codes
}
}
I think you have to use "return null;" instead of just "return;", as Void is a class around the usual "void" value.
In doInBackGround return null, as Void is an object (does not = void)..
public class BackgroungTask extends AsyncTask<String, Void, Void> {
public Void doInBackground(String... params) {
//tasks
return null; //error occurs here!
}
public void onPostExecute(Void result) {
//codes
}
}
AsyncTask works like this
onPreExecute -> doInBackGround -> onPostExecute
Neglecting progressUpdates etc, as soon doInBackGround is complete, control goes to onPostExecute. A simple return disrupts this flow hence causing the error. To Solve this, replace with return null Since its a void. The return statement parameters are passed to onPostExecute as parameter, where you can use it to see wheter it has been successful.
doInBackGround returns null value
public class BackgroungTask extends AsyncTask<String, Void, Void> {
....
....
}
check this Android dev doc for asynchronous task in andorid