Android Studio 如何将授权标头添加到IO,Android中套接字调用

0wi1tuuw  于 2023-05-01  发布在  Android
关注(0)|答案(2)|浏览(64)
JSONObject getobj = new JSONObject();

        postobj.put("url", String.format("/user"));

        Log.d("Get MEthod", getobj.toString() + ">>>>>>>>>>>>>>>>>>>>Get Message " + socket.connected());

        Log.d(TAG, " the meessage url " + getobj.toString());

        socket.emit("get",getobj, new Ack() {
            @Override
            public void call(Object... args) {

                JSONObject obj = (JSONObject) args[0];

                Log.d("GET CHAT MEESSAGE ", obj.toString() + ">>>>>>>>>>>>>>>>>>>> with in call method Get Message " + socket.connected());

            }
        });

/* 我得到了Bellow响应 *\

{
   "body": {
      "err": "No Authorization header was found"
     },
   "headers": {

   },
   "statusCode": 401
  }
guicsvcw

guicsvcw1#

这就是我如何为套接字连接添加授权令牌的方法,希望对您有所帮助

IO.Options options = new IO.Options(); 
options.forceNew = true;                        
options.reconnectionAttempts = Integer.MAX_VALUE;                    
options.timeout = 10000;    
options.query = "token=" + "your_authorization_code";
qyswt5oh

qyswt5oh2#

对于传递头是IO。套接字,我们需要创建选项

val options = Options
            .builder()
            .setExtraHeaders(
                mapOf("Authorisation" to listOf("TOKEN"))
            )

然后把它传给IO。插座

IO.socket("URL", options)

相关问题