3

我的应用程序经常与java.lang.IllegalArgumentException: Code required to include reason. 我已尽力减少运行线程的服务的负载,该线程将客户端应用程序连接到node.js服务器,但我仍然收到此错误。我认为异常是由于我尝试收听时的通信错误而发生的,但我不确定。

这是我的Service课。


public class GlobalSocketWithEventBus extends Service {
    NetworkStateChange networkStateChange;
    Socket socket;
    Socket socket2;
    String token;
    GPSTracker gpsTracker;
    SharedPreferences prefToken;
    SharedPreferences prefUser;
    Handler handler = new Handler();


    @Override
    public void onCreate() {
        super.onCreate();
        try {
            socket = IO.socket("https://hubryde-trip-service.herokuapp.com/");
            socket2 = IO.socket("https://hubryde-request-service.herokuapp.com/");
            Log.d("socket_", "Connected to Socket_Here");
            connectToWebSocket();
            handler.post(sendData);

        } catch (URISyntaxException e) {
            Log.d("ServerTimeOut", "Server Time out");
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        prefUser = getApplicationContext().getSharedPreferences("UserProfile", 0);

        networkStateChange  = new NetworkStateChange();
        prefToken = getApplicationContext().getSharedPreferences("MyPref", 0);
        token = prefToken.getString("TOKEN", null);
        assert token != null;

        socket.
        once("allRequests"+prefUser.getString("EMAIL", null), args ->
        {
            // Log.d("Server_Socket_dashboard", "Coming in");
            JSONObject result = (JSONObject) args[0];
            //Log.d("AweleSocket_br_Service", result.toString());
            //extract data from fi red event

            //socket2.disconnect();
            EventBus.getDefault().post(
                    new EventBusModel(result));
            // extractObject(data);

        });

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    public void connectToWebSocket() {
        socket.connect();
        socket2.connect();
    }



        public void emitEventBus()  {

            gpsTracker = new GPSTracker(getApplicationContext());
            //REPEAT A METHORD AT SPECIFIC INTERVALS
            JSONObject obj = new JSONObject();
            Log.d("ProfileDashBaord_socket","Connected to Socket_Here");
            try {
                token = prefToken.getString("TOKEN", null);
                obj.put("token", token);
                obj.put("email",  prefUser.getString("EMAIL", null));
                obj.put("latitude", String.valueOf(gpsTracker.getLatitude()));
                obj.put("longitude", String.valueOf(gpsTracker.getLongitude()));
                //obj.put("bearing", String.valueOf(gpsTracker.getBearing()));
            } catch (JSONException e) {
                Log.d("ServerTimeOut", "Server Time out");
            }

           // Log.d("Board_emit",obj.toString());
            // Receiving an object

            socket.emit("busRequests", obj);

            //socket.connect();
           // Log.d("ProfileDashBoard_emit",obj.toString());
            //connect you socket client to the server

        //connect you socket client to the server
    }

    public void sendBusLocationToServer() {

        gpsTracker = new GPSTracker(getApplicationContext());
        try {
            JSONObject obj = new JSONObject();
            obj.put("latitude", gpsTracker.getLatitude());
            obj.put("token", token);
            obj.put("longitude", gpsTracker.getLongitude());
            obj.put("bearing", gpsTracker.getBearing());
            Log.d("EMMITTING", "emitting");
            obj.put("phone", prefUser.getString("PHONE", null));

            //Log.d("busLocation", String.valueOf(obj));
            //Log.d("busLocation_board", String.valueOf(obj));

            socket2.emit("busLocation", obj);
            //socket2.connect();

        } catch (JSONException e) {
            e.printStackTrace();
        }
        //connect you socket client to the server
    }

    public void freeMemory(){
        System.runFinalization();
        Runtime.getRuntime().gc();
        System.gc();
    }

这就是我每次发出时处理线程以监听来自服务器的响应的方式。

   private final Runnable sendData = new Runnable(){
        public void run(){
            try {

                emitEventBus();
                sendBusLocationToServer();
                Log.d("RERUNINGPAGE", "YES" );

                handler.removeCallbacks(sendData);
                //prepare and send the data here..
                if (socket.connected()) {
                    Log.d("socket_connected_test", "connected");
                    socket.disconnect();
                    socket2.disconnect();
                } 
                handler.postDelayed(this, 5000);

            }
            catch (Exception e) {
                Log.d("Error", "exceeption1");
            }
        }
    };

以防我使用该服务离开当前活动,我像这样释放了内存。

   @Override
    public void onDestroy() {
        super.onDestroy();
        //handler.removeCallbacks(sendData);

        stopSelf();

        freeMemory();
    }

不知何故,某处发生了一些奇怪的事情,我得到了这个我不熟悉的错误。它实际上指向我正在听的。

E/AndroidRuntime: FATAL EXCEPTION: OkHttp https://hubryde-trip-service.herokuapp.com/socket.io/?EIO=3&sid=ed3R8QhpbTg5tNncAACS&transport=websocket WebSocket
    Process: com.project.hubrydemanagerapp, PID: 2582
    java.lang.IllegalArgumentException: Code required to include reason.
        at com.squareup.okhttp.internal.ws.WebSocketWriter.writeClose(WebSocketWriter.java:110)
        at com.squareup.okhttp.internal.ws.RealWebSocket.peerClose(RealWebSocket.java:146)
        at com.squareup.okhttp.internal.ws.RealWebSocket.access$100(RealWebSocket.java:31)
        at com.squareup.okhttp.internal.ws.RealWebSocket$1$2.execute(RealWebSocket.java:74)
        at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
4

0 回答 0