Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package ru.chemnote.zheev.golos;
  2.  
  3. import android.util.Log;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. import java.util.Objects;
  10.  
  11. import okhttp3.Response;
  12. import okhttp3.WebSocket;
  13. import okhttp3.WebSocketListener;
  14.  
  15. /**
  16.  * Created by zheev on 07.03.18.
  17.  */
  18.  
  19. public class WebSocketGolos extends WebSocketListener {
  20.  
  21.     @Override
  22.     public void onOpen(WebSocket webSocket, Response response) {
  23.  
  24.         try {
  25.             JSONObject jsonObject = new JSONObject("{id:1, method:'call', 'params': ['database_api', 'set_block_applied_callback', [0] ]}");
  26.  
  27.             webSocket.send(jsonObject.toString());
  28.         } catch (JSONException e) {
  29.             e.printStackTrace();
  30.         }
  31.  
  32.     }
  33.  
  34.     @Override
  35.     public void onMessage(WebSocket webSocket, String text)
  36.     {
  37.         try{
  38.             JSONObject data = new JSONObject(text);
  39.             String method = data.getString("method");
  40.             Log.d("WS", method.toString());
  41.             JSONArray params = data.getJSONArray("params");
  42.             JSONArray objectParam = params.getJSONArray(1);
  43.             JSONObject arrayObjectParam = objectParam.getJSONObject(0);
  44.  
  45.             String previousParam = arrayObjectParam.getString("previous");
  46.  
  47.             Integer hex = Integer.parseInt(previousParam.substring(0, 8), 16);
  48.  
  49.             Log.d("WS", hex.toString());
  50.             if(Objects.equals(method, "notice")){
  51.                 Log.d("WS", data.toString());
  52.                 System.out.print(data);
  53.             }
  54.  
  55.  
  56.         }catch (JSONException e){
  57.             e.toString();
  58.         }
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement