gpt4 book ai didi

java - java中使用jetty发送json对象

转载 作者:行者123 更新时间:2023-12-02 11:05:40 26 4
gpt4 key购买 nike

我不明白为什么什么也没有发生。

我正在尝试编写一个java程序,将json对象发送到jetty服务器。服务器已经编写好了(由其他人编写,它是一个项目),并且仅排除某些 json 对象。但他没有从我的程序中得到任何东西。

public class client {
final static String HOST = "localhost";
final static int PORT = 3000;

public static void main(String[] args)
{

String URIString = "ws://" + HOST + ":" + PORT + "/servlets";
URI uri = URI.create(URIString);
WebSocketClient client = new WebSocketClient();

JSONObject js = new JSONObject();

js.put("toke","hallo");



Clientsocket clientsocket = new Clientsocket();
try {
client.start();
Future<Session> fut = client.connect(clientsocket, uri);

clientsocket.getSession().getRemote().sendString(js.toJSONString());;


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

public class Clientsocket extends WebSocketAdapter {

//private static final Logger LOG = Log.getLogger(Clientsocket.class);

@Override
public void onWebSocketClose(int statusCode,String reason)
{
super.onWebSocketClose(statusCode, reason);
//LOG.info("Websocket Close:{} - {} ", statusCode,reason);
}


@Override
public void onWebSocketConnect(Session session)
{
super.onWebSocketConnect(session);
//LOG.info("Websocket Connect: {}", session);

}


}

启动时出现错误消息:

2018-06-22 14:26:43.789:INFO::main: Logging initialized @257ms to org.eclipse.jetty.util.log.StdErrLog
java.lang.NullPointerException


[both classes: clientsocket, client][1]

最佳答案

我认为您没有等待连接完成。

Future<Session> fut = client.connect(clientsocket, uri);

Session session = fut.get(); // wait for connect to complete (or throw exception)

session.getRemote().sendString(js.toJSONString());

关于java - java中使用jetty发送json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com