【问题标题】:Android - Autobahn Websocket sending message error(NullPointerException)Android - Autobahn Websocket 发送消息错误(NullPointerException)
【发布时间】:2013-06-19 12:11:33
【问题描述】:

我正在与 Autobahn 进行 websocket 通信。 在我的应用程序的 main.class 中,我设置为在用户单击按钮时调用“connect()”。

// Toggle Button event
    tButton = (ToggleButton) findViewById(R.id.toggleButton1);    
    tButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if(isChecked){

            }else{

            }

        }
    });

然后是MyOffers.class,如果访问这个类, MyOffers_Fragment 类自动生成四次,因为 MyOffers.class 包含“轮播视图”并且有四个产品。

在“MyOffers_Fragment”类中,当用户单击产品图片之一时,应发送消息。

if (pos == 0) {
    product_photo.setImageResource(R.drawable.myoffers_0);
    product_photo.setOnClickListener(new ImageButton.OnClickListener(){
        public void onClick(View v){
            String id = "Product0";
            Log.d(TAG, "Current product is : " + id);
            A.sendMessage(id);  
        }
    });
}

但是'mConnection.sendTextMessage(id1);'此行会产生“NullPointerException”错误。 有一个类'Websocket_Connector.class'

public class WebSocket_Connector {

    private static final String TAG = "ECHOCLIENT";
    public final WebSocketConnection mConnection = new WebSocketConnection();

    public void connect(final String wsuri) {

          Log.d(TAG, "Connecting to: " + wsuri); 

          try {
             mConnection.connect(wsuri, new WebSocketHandler() {

                @Override
                public void onOpen() {
                   Log.d(TAG, "Status: Connected to " + wsuri ); 
                   Log.d(TAG, "Connection successful!\n");
                }

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

                @Override
                public void onClose(int code, String reason) {
                   Log.d(TAG, "Connection closed.");
                }
             });
          } catch (WebSocketException e) {

             Log.d(TAG, e.toString());
          }

     public void sendMessage(String message) {
        connect("ws://192.168.x.xxx:xxxx");
        mConnection.sendTextMessage(message); 
     }

 }

我在主页类中调用了“connect()”,然后尝试发送消息。 但是,它不起作用..你能帮帮我吗?

【问题讨论】:

  • 从哪里获得 NPE?
  • @blackbelt mConnection.sendTextMessage(id1);这一行
  • WebSocketConnection 是你自己的类吗?
  • 不,它在高速公路图书馆。 "autobahn.ws/static/reference/android/…"
  • @blackbelt 当然,我在我的项目中添加了这个库。

标签: android nullpointerexception websocket autobahn


【解决方案1】:
public class WebSocket_Connector {

private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();
private String tmpString = "";
public void connect(final String wsuri) {

      Log.d(TAG, "Connecting to: " + wsuri); 

      try {
         mConnection.connect(wsuri, new WebSocketHandler() {

            @Override
            public void onOpen() {
               Log.d(TAG, "Status: Connected to " + wsuri ); 
               Log.d(TAG, "Connection successful!\n");
               mConnection.sendTextMessage(tmpString); 
               tmpString = "";
            }

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

            @Override
            public void onClose(int code, String reason) {
               Log.d(TAG, "Connection closed.");
            }
         });
      } catch (WebSocketException e) {

         Log.d(TAG, e.toString());
      }

 public void sendMessage(String message) {

    if (mConnection.isConnected()) 
            mConnection.sendTextMessage(message); 
    else {
       tmpString = message;
       connect("ws://192.168.x.xxx:xxxx");
    }

 }

 }

【讨论】:

  • 对其进行了修改并“部分”工作。问题是每次我点击图片时它都会连接到服务器。
  • 有没有什么方法可以让我连接服务器,然后发送消息?
  • 你为什么相信它每次都连接到服务器?
  • 在服务器上,我设置了客户端连接服务器时显示客户端的日志。当我点击图片时,服务器日志显示每条消息都是由不同的客户端发送的。
  • 查看库代码我发现了这条评论 // 如果已经连接,则不要连接 .. 用户需要先断开连接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 2016-12-19
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多