0

我已经集成了 AutoBahn 来满足我在我的 android 应用程序中的 WebSocket 需求。这在大多数情况下都很有效。

有时我会因为这个日志而崩溃:

java.lang.NullPointerException
            at de.tavendo.autobahn.WebSocketConnection.sendTextMessage(WebSocketConnection.java:137)
            at com.test.testSockets.websockets.Request.GetForceResultsRequest$1.onOpen(GetForceResultsRequest.java:48)
            at de.tavendo.autobahn.WebSocketConnection$1.handleMessage(WebSocketConnection.java:370)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5653)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method) 

这是我的 GetForceResultRequest 类:

public class GetForceResultsRequest extends BaseRequest {
    protected final WebSocketOptions mWebSocketOptions = new WebSocketOptions();
    private static String m_TAG = GetForceResultsRequest.class.getSimpleName();
    private String mRequest = "{\"request\":\"getForceTestResults\"}";
    private String mSensorId;


    public GetForceResultsRequest(String hostAddress, RemoteCallListener callListener){
        mSocketHostAddress = hostAddress;
        listener = callListener;
        mAutoBahnConnection = new WebSocketConnection();

        mWebSocketOptions.setReceiveTextMessagesRaw(true);
        mWebSocketOptions.setSocketReceiveTimeout(100000);
        mWebSocketOptions.setSocketConnectTimeout(100000);

        //mWebSocketOptions.setTcpNoDelay(true);

        Log.i(m_TAG, mRequest);
    }

    @Override
    protected Void doInBackground(Void... params){

        try {
            mAutoBahnConnection.connect(mSocketHostAddress,new String[]{mProtocol} ,new WebSocketHandler(){

                @Override
                public void onOpen() {
                    Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
                    mAutoBahnConnection.sendTextMessage(mRequest);
                }

                @Override
                public void onTextMessage(String payload) {
                    Log.i(m_TAG, "Got echo: " + payload);
                }

                @Override
                public void onRawTextMessage(byte[] payload) {

                    try {
                        if(payload!=null) {
                            mJsonResponse = new String(payload, "UTF-8");
                            Log.i(m_TAG, mJsonResponse);
                            parseJson(mJsonResponse, GetForceTestResultResponse.class);
                        }

                        //parseJson(mJsonResponse);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        Log.i(m_TAG, e.toString());
                        listener.onRemoteErrorOccur(e);
                    } catch (IOException e) {
                        Log.i(m_TAG, e.toString());
                        listener.onRemoteErrorOccur(e);
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        Log.i(m_TAG, e.toString());
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        Log.i(m_TAG, e.toString());
                        e.printStackTrace();
                    }
                }

                @Override
                public void onBinaryMessage(byte[] payload) {
                    Log.i(m_TAG, "ON BINARY MESSAGE");
                }


                @Override
                public void onClose(int code, String reason) {
                    Log.i(m_TAG, "Connection lost."+ reason);

                }
            }, mWebSocketOptions);
        } catch (WebSocketException e) {
            Log.d(m_TAG, e.toString());
            listener.onRemoteErrorOccur(e.toString());
        }
        return null;
    }
}

我在倒数计时器中每秒调用 5 次。此倒计时计时器运行 30 秒

所以这里是方法

  private boolean forceTestPoll(){

        WebServices.getInstance().getForceTestResults(m_HostAddress, new RemoteCallListener() {
            @Override
            public void onRemoteCallComplete(Object result) {
                GetForceTestResultResponse resultResponse = (GetForceTestResultResponse)result;
                List<SensorForceTestResult> list = resultResponse.getSensors();

                for(SensorForceTestResult forceTestResult: list){
                        if(forceTestResult.getForceResult()==1){
                            mForceDetected = true;
                        }else {
                            mForceDetected = false;
                        }
                }
            }

            @Override
            public void onRemoteErrorOccur(Object error) {
                Log.i(m_Tag, "ERROR: " +(String)error);
            }
        });
        return mForceDetected;}

这在 countdowntimer 的 onTick 方法中调用,每秒调用 5 次。

所以 forceTestPoll() 返回一个布尔值,所以它被称为:

if(forceTestPoll()){
//processing here
}

因此,如果有人能解释为什么我有时会收到此错误,我将不胜感激?我打电话太频繁了吗?

编辑

所以下面是扩展的基本请求

public class BaseRequest extends AsyncTask<Void, Void, Void>{

    protected static WebSocketConnection mAutoBahnConnection;
    protected String mSocketHostAddress;
    protected String mJsonResponse;

    protected RemoteCallListener listener;
    protected static final String  mProtocol = "sensor_tester_api_v2";


    @Override
    protected Void doInBackground(Void... params) {return null;}

    @Override
    protected void onPostExecute(Void v){}

    protected void parseJson(String jsonResponse, Object WebSocketsResponse) throws IllegalAccessException, InstantiationException, IOException {
       Object response = WebSocketsResponse;
        response =  new ObjectMapper().readValue(jsonResponse, (Class<Object>) response);
        listener.onRemoteCallComplete(response);
    }
}
4

0 回答 0