按websocket响应的名称获取值(字符串)

时间:2017-11-15 07:23:55

标签: java android websocket

如何通过websocket响应(字符串)的名称获取值,我已经尝试将响应转换为jsonArray但它没有工作。 那么如何通过键(任务)获得值(拖把楼层)?我正在使用android studio开发应用程序。输出时出现问题("要执行的任务:" + text.get(" task"));

private final class EchoWebsocketListener extends WebSocketListener{
    @Override
    public void onOpen(WebSocket webSocket, okhttp3.Response response) {
        webSocket.send("{\n" +
                "   \"task\": \"Mop floor\",\n" +
                "   \"time_limit\": \"1\"\n" +
                "}");

    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {
        output("Task to do: " +text.get("task"));
    }

    @Override
    public void onClosed(WebSocket webSocket, int code, String reason) {
        webSocket.close(1000,"Goodbye!");
    }

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

0 个答案:

没有答案