OkHttpClient 抛出 NullPointerException

OkHttpClient throws NullPointerException

我正在尝试使用 OkHttp3 通过 WiFi 发送一些数据。

我使用

创建了一个客户端
import okhttp3.OkHttpClient;
.....
public OkHttpClient client;

点击按钮后我设置了 websocket,

private void start() {
    Request request = new Request.Builder().url("ws://192.168.1.84:81/").build();
    EchoWebSocketListener listener = new EchoWebSocketListener();
    WebSocket ws = client.newWebSocket(request, listener);

    client.dispatcher().executorService().shutdown();
}

其中 EchoWebSocketListenerWebSocketListener 的扩展,完整列表如下。

但是我在 运行 时不断收到以下错误...

java.lang.NullPointerException: Attempt to invoke virtual method 'okhttp3.WebSocket okhttp3.OkHttpClient.newWebSocket(okhttp3.Request, okhttp3.WebSocketListener)' on a null object reference

当追溯它在 start() 函数中的行时,

WebSocket ws = client.newWebSocket(request, listener);

为什么clientnull

完整代码清单:

package qdivision.org.qrtracker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

public class Move extends AppCompatActivity {

    public OkHttpClient client;
    public TextView output;
    private ImageButton forward;
    public String dataFrame;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move);
        Intent intent = getIntent();

        forward  = (ImageButton) findViewById(R.id.forward_button);
        forward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dataFrame = "<192.168.1.84,CMD,KEY,57,33>";
                start();
            }
        });
    }

    private final class EchoWebSocketListener extends WebSocketListener {
        private static final int NORMAL_CLOSURE_STATUS = 1000;

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            webSocket.send(dataFrame);
            webSocket.close(NORMAL_CLOSURE_STATUS, "Goodbye !");
        }

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            output("Receiving : " + text);
        }

        @Override
        public void onMessage(WebSocket webSocket, ByteString bytes) {
            output("Receiving bytes : " + bytes.hex());
        }

        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            webSocket.close(NORMAL_CLOSURE_STATUS, null);
            //output("Closing : " + code + " / " + reason);
        }

        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            output("Error : " + t.getMessage());
        }
    }

    private void start() {
        Request request = new Request.Builder().url("ws://192.168.1.84:81/").build();
        EchoWebSocketListener listener = new EchoWebSocketListener();
        WebSocket ws = client.newWebSocket(request, listener);

        client.dispatcher().executorService().shutdown();
    }

    private void output(final String txt) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                output.setText(output.getText().toString() + "\n\n" + txt);
            }
        });
    }

}

您忘记初始化客户端,因此客户端变量仍然为空。这样做:

private void start() {
        //Here we initialize client variable
        client = new OkHttpClient();

        Request request = new Request.Builder().url("ws://192.168.1.84:81/").build();
        EchoWebSocketListener listener = new EchoWebSocketListener();

        // Then we could use it like so:
        WebSocket ws = client.newWebSocket(request, listener);    
        client.dispatcher().executorService().shutdown();
    }