0

我正在与 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 包含'carousel view',并且有四个产品。

在“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()”,然后尝试发送消息。但是,它不起作用..你能帮帮我吗?

4

1 回答 1

0
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");
    }

 }

 }
于 2013-06-19T15:44:29.493 回答