Runnable error After Asynchtask replacement

Viewed 28

I am getting an error on the last line below after replacing my asynctask with a runnable to upload data to server: Any pointers will be appreciated.

Cannot return a value from a method with void result type

on this line

return *imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
ExecutorService service = Executors.newSingleThreadExecutor ( );
        service.execute ( new Runnable ( ) {
            @Override
            public void run() {
            //preexecute
                runOnUiThread ( new Runnable ( ) {
                    @Override
                    public void run() {
                     progressDialog = ProgressDialog.show( UploadActivity.this,"Your Data is Uploading To Our Servers",
                     "Please Wait And Ensure Data is On With Bundles. 3G or WiFi data is preferred",false,false);
                    }
                } );
            //doinbackground
                ImageProcessClass imageProcessClass = new ImageProcessClass ();
                HashMap<String,String> HashMapParams = new HashMap<>();
                HashMapParams.put(USER_NAME, userName);
                HashMapParams.put(USER_EMAIL, userEmail);
                HashMapParams.put(USER_PHONE, userPhone);
                HashMapParams.put(ImagePath1, imageView1);
                              
                return imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);
1 Answers

The problem is that run() has no return value where your previous solution with AsyncTask allows a return value.

I expect this return something.

imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);

but this has void as a return value

@Override
public void run() {
... 
}

I think you need a new solution for returning the result, maybe working with a Listener and trigger the callback?

interface ResultListener{
     public void onResult(result: ResultType)
}

@Override
public void run() {

    ...
    listener.onResult(imageProcessClass.ImageHttpRequest(ServerUploadPath, HashMapParams);)

}

// Sorry I'm not very familiar with Java anymore, hope you get the idea :)
Related