0

I am new to android and java programming but I have been given a task to finish an app that a contractor started. I looked up and found how to call an ASync task and wait for it to finish but I can't get it to work. In fact, it won't even compile. I copied it right from one of the answers in How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

The errors I get are below. They come in MyCalling class. I appended ***** to the end of the line that has the error

error:

   processFinish(String) in <anonymous 
   com.zoeller.z_controlmobile.MyCallingClass$2> cannot implement 
   processFinish(String) in AsyncResponse
   attempting to assign weaker access privileges; was public

and

   error: incompatible types: AsyncTask<String,String,String> cannot be 
   converted to MyASyncClass

My code is below

public class MyCallingClass extends ActivityBase {
    private String _result = null;

    protected void onCreate(Bundle savedInstanceState) {
        callingMethod();
    }

    protected void callingMethod() {
        MyASyncClass whatever = new MyASyncClass(new MyASyncClass.AsyncResponse() {
            @Override
            void processFinish(String output) { *****
                _result = output;
            }
        }).execute();
    }

    // More work done here
}

public class MyASyncClass extends AsyncTask<String, String, String> {
    public interface AsyncResponse {
        void processFinish(String output);
    }

    public AsyncResponse delegate = null;

    public DeviceConnect(AsyncResponse delegate){
        this.delegate = delegate;
    }

    @Override    
    protected String doInBackground(String... params) {
    // Does the work
    }

    @Override
    public void onPostExecute(String result) {
        delegate.processFinish(result);
    }
}
Marty G
  • 39
  • 4

2 Answers2

0
public class MyCallingClass extends ActivityBase {
private String _result = null;

protected void onCreate(Bundle savedInstanceState) {
    callingMethod();
}

private void callingMethod() {
    MyASyncClass whatever = new MyASyncClass(output -> _result = output);
    whatever.execute();
}
}

class MyASyncClass extends AsyncTask<String, String, String> {
public interface AsyncResponse {
    void processFinish(String output);
}

private AsyncResponse delegate = null;

MyASyncClass(AsyncResponse delegate){
    this.delegate = delegate;
}

@Override
protected String doInBackground(String... params) {
    // Does the work

    return "";
}

@Override
public void onPostExecute(String result) {
    delegate.processFinish(result);
}
}
Julkar Nain
  • 41
  • 1
  • 5
0

When you're declaring an interface with the following code:

public class MyASyncClass extends AsyncTask<String, String, String> {

    public interface AsyncResponse {
        void processFinish(String output);
    }

     ...
}

the access modifier of void processFinish(String output) is implicitly assigned to public. So, what you're really get is:

public class MyASyncClass extends AsyncTask<String, String, String> {

    public interface AsyncResponse {
        public void processFinish(String output);
    }

    ...
}

When you're constructing an object from the interface, you need to override both the method and the access modifier. Something like this:

MyASyncClass.AsyncResponse response = new MyASyncClass.AsyncResponse() {
        @Override
        public void processFinish(String output) {
           // do something with the output.
        }
    };
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96