无法调用ClientEndpoint Websocket的类中的方法

时间:2018-08-22 04:42:25

标签: java websocket android-studio-3.0

我正在使用android studio。我有两个Java类:Mainactivity.java和WSClient.java。基本上,我要做的就是从MainActivity类中引用WSClient类中的变量“ feedback”,以便可以在文本视图上显示它……并在MainActivity类中对其进行进一步的操作。

以下是WSClient.java类:

package com.example.smijes.binarydatatrials;

import java.net.URI;
import java.io.IOException;
import java.lang.InterruptedException;
import javax.websocket.*;

@ClientEndpoint
public class WSClient  {

    public static String feedback;

    @OnOpen
    public void onOpen(Session session) throws java.io.IOException
    {
        session.getBasicRemote().sendText("{\"ticks\": \"R_100\"}");
    }

    @OnMessage
    public void onMessage(String message)
    {
        feedback = message;
        System.out.println(feedback);

    }

    public static void main(String[] args)
            throws IOException, DeploymentException, InterruptedException
    {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        URI apiUri = URI.create("wss://ws.binaryws.com/websockets/v3?app_id=1089");
        Session session = container.connectToServer(WSClient.class, apiUri);
        Thread.sleep(10000);
    }
}

以下是WSClient.java类:

package com.example.smijes.binarydatatrials;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import javax.websocket.*;

public class MainActivity extends AppCompatActivity {

    TextView displayTv;
    static Button loaderBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        displayTv = (TextView) findViewById(R.id.display_tv);
        loaderBt = (Button) findViewById(R.id.loader_bt);

        loaderBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    WSClient.main(new String[]{});
                } catch (IOException e) {
                    displayTv.setText(displayTv.getText().toString() + "\n IOException");
                } catch (DeploymentException e) {
                    displayTv.setText(displayTv.getText().toString() + "\n DeploymentException");
                } catch (InterruptedException e) {
                    displayTv.setText(displayTv.getText().toString() + "\n InterruptedException");
                }
                if (WSClient.feedback != null) {
                    displayTv.setText(displayTv.getText().toString() + "\n" + WSClient.feedback);
                }else {
                    displayTv.setText(displayTv.getText().toString() + "\n There's no feedback to display");
                }

            }
        });
    }
}

当我在Android设备上安装应用程序并单击loaderBt时,显示以下内容:

DeploymentException
There's no feedback to display

我知道这意味着我调用的WSClient.main方法捕获了Deployment异常,因此没有完全执行,因此将我的反馈变量保留为null。 拜托,我哪里出问题了?我该如何纠正?

0 个答案:

没有答案