java - android使用websocket無法訪問cookie。
問題描述
我的java服務端為了分清各個用戶使用session里的用戶名做了區分,html頁面使用沒有問題,但是在android端訪問的時候取不到cookie值。
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; // 鏈接webSocket時傳遞的參數 標識是否是特定分組, Null表明不是 //String type = (String) servletRequest.getServletRequest().getParameter('type'); HttpSession session = servletRequest.getServletRequest().getSession(false); if (session != null) {LoginUserInfo user = (LoginUserInfo) session.getAttribute('loginUserInfo');//System.out.println('id');//System.out.println(session.getId());if (user != null) { attributes.put('user', user); //attributes.put('type', type);Enumeration<String> params = servletRequest.getServletRequest().getParameterNames(); while (params.hasMoreElements()) {String param = params.nextElement();attributes.put(param, servletRequest.getServletRequest().getParameter(param)); }} }}return true; }
//websocket建立
WebSocketClient websocket;/** * 與服務器建立websocket連接 */private void websocket() { new Thread(new Runnable() {@Overridepublic void run() { //獲取之前的登錄信息 String Cookie = cookie.get(0).getName()+'='+cookie.get(0).getValue()+';'+ cookie.get(4).getName()+'='+cookie.get(4).getValue()+';'+ cookie.get(5).getName()+'='+cookie.get(5).getValue()+';'; application.setCookie(Cookie); Log.i('', Cookie); try {HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put('Cookie', Cookie);// 這里的url不能帶有http://// 直接使用 xx.xx.xx.xx:端口websocket = new WebSocketClient(new URI(HttpUrlS.WEBSOCKET), new Draft_17(), hashMap, 5000) { @Override public void onOpen(ServerHandshake handshakedata) {Log.d('MyTAG', 'WebSocketClient onOpen'); } @Override public void onMessage(final String message) { //WebSocketUtil.handleMessage(message);Log.i('', '有人說:' + message);runOnUiThread(new Runnable() { @Override public void run() { //Toast.makeText(MainActivity.this, '有人說:' + message, Toast.LENGTH_SHORT).show(); }}); } @Override public void onClose(int code, String reason, boolean remote) {Log.d('MyTAG', 'close = ' + reason); } @Override public void onError(Exception ex) {ex.printStackTrace();Log.d('MyTAG', 'onError = ' + ex.toString()); }};websocket.connect(); } catch (URISyntaxException e) {e.printStackTrace(); }} }).start();}
問題解答
回答1:他沒傳吧,自定義的loginUserInfo需要android手動傳
回答2:你好,請問一下Android怎么自定義的loginUserInfo手動傳?
相關文章:
1. html - 前端大牛都頂一下!CSS鼠標樣式問題,如圖所示2. html5 - canvas中的mousedrag事件,為什么鼠標拖出canvas,然后再次移入canvas,drag事件還觸發3. node.js - 在nodejs環境中如何配置webhook以實現hexo博客的自動更新?4. 網頁爬蟲 - python爬蟲用BeautifulSoup爬取<s>元素并寫入字典,但某些div下沒有這一元素,導致自動寫入下一條,如何解決?5. 百度地圖api - Android百度地圖SDK,MapView上層按鈕可見卻不可觸,怎么解決?6. mysql 獲取時間函數unix_timestamp 問題?7. javascript - es6擴展運算符...的問題8. 新入手layuiadmin,部署到tp中。想用php自已寫一個后臺管理系統。9. javascript - jquery怎么給select option一個點擊時觸發的事件,如圖 如果選擇自定義觸發一個時間?10. css3 - 求教CSS圖標庫的寫法,也就是先做雪碧圖,然后寫一個css表,用的時候直接用class就可以了
