'SignalR Java Client not working in Android 12
I implemented SignalR Java Client in my Android Project. It works well on Android Versions 6 to 11 but fails on Android 12, I'm getting this error only on Android 12 java.net.SocketException: Socket closed [Ljava.lang.StackTraceElement;@e95116d, here is my code:
import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.OnClosedCallback;
import org.json.JSONObject;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import timber.log.Timber;
public class SignalRChatService {
Timer timer = new Timer();
public void startTimer()
{
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            // Your code here
            Timer_Elapsed();
        }
    }, 1000, 2000);
}
private void Timer_Elapsed()
{
    try
    {
        module.checkNetwork(context);
        if(!Connected)
        {
            Init();
        }
    }
    catch (Exception e)
    {
        Connected = false;
    }
}
private HubConnection _connection;
public boolean Connected;
public String instanceName;
public int instanceProfileId;
private boolean connecting;
private SignalRMessageReceivedEvents signalRMessageReceivedEvents;
private final Gson gson;
private final Context context;
public SignalRChatService(Context context, String instanceName, int instanceProfileId, SignalRMessageReceivedEvents signalRMessageReceivedEvents) {
    this.instanceName = instanceName;
    this.instanceProfileId = instanceProfileId;
    this.connecting = false;
    this.context = context;
    this.signalRMessageReceivedEvents = signalRMessageReceivedEvents;
    gson = new GsonBuilder().setPrettyPrinting().create();
}
public void Stop()
{
    try
    {
        _connection.stop();
    }
    catch(Exception ignored)
    {}
}
public void Init()
{
    if (connecting)
    {
        return;
    }
    try
    {
        connecting = true;
        _connection  = HubConnectionBuilder.create("https://" + instanceName.trim().toLowerCase() + ".com").build();
        try
        {
            _connection.start();
            Connected = true;
        }
        catch (Exception e)
        {
            Connected = false;
            Timber.e("SignalR Push connect: %s", e.getLocalizedMessage());
            return;
        }
        _connection.on("Message", (message) ->
        {
            try
            {
                // Separate this code affterwads
                JSONObject messageObject =  new JSONObject(message);
                String Messagetype = (String) messageObject.get("messageType");
                HandleWebSocketMessage(Messagetype, message);
            }
            catch (Exception ignored)
            {
            }
        }, String.class);
        _connection.onClosed(new OnClosedCallback() {
            @Override
            public void invoke(Exception exception) {
                handleClosed();
            }
        });
    }
    catch (Exception ignored)
    {
    }
    finally
    {
        connecting = false;
        startTimer();
    }
}
private void handleClosed()
{
    try
    {
        TimeUnit.MILLISECONDS.sleep(100);
    }
    catch (Exception ignored)
    {
    }
    Init();
}
}
I've tried upgrading to the latest version of SignalR Java Client
 implementation "com.microsoft.signalr:signalr:6.0.3"
and it is still not working. It just wont receive any Signal.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|
