Skip to content
Advertisement

How to fix the Issue with replacing AsyncTask to connect socket?

I’m trying to use This approach(Marked Answer) to replace my code with AsyncTask to make connection between user in android and server, And here’s what I’ve done :

Note: I’m not using any specific pattern, just trying to achieve this goal on a basic structure.

MainActivity :

JavaScript

ClientConnection :

JavaScript

ConnectionTask :

JavaScript

ConnectionRunningTask:

JavaScript

So base on the above code I’ll try to explain what I’ve tried and what is the current problem :

Problem: base on the above code I’m getting the below error in connect() which it is telling me data is R and you can’t set it to serverSocket, and due to this error I can’t build the application.

Then I’ve tried to replace all R with Socket in the ConnectionTask class ( R in Callback and Callable), which in this case application was able to build and run.

JavaScript

But based on the above approach I’ll get an error when I hit connect button, and here’s what I’ve got :

JavaScript

On the Server-Side I’ve also checked incoming connections, and nothing happened there…

Advertisement

Answer

You’re not instantiating taskRunner inside ClientConnection. Add this line to the constructor:

JavaScript
Advertisement