Advertisement
Guest User

Untitled

a guest
Mar 10th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 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.         JSONObject data = null;
  38.  
  39.         try {
  40.  
  41.             data = new JSONObject(text);
  42.  
  43.             if (data.has("id")){
  44.                 int idRequest = data.getInt("id");
  45.  
  46.                 Log.d("WS", Integer.toString(idRequest));
  47.  
  48.                 if(idRequest == 2) {
  49.  
  50.                     this.getContent(data);
  51.  
  52.                 }
  53.  
  54.             }else if(data.has("method")){
  55.  
  56.                 String method = data.getString("method");
  57.  
  58.                 if(Objects.equals(method, "notice")){
  59.  
  60.                     int hex = this.getNumberBlock(text);
  61.  
  62.                     try {
  63.  
  64.                         JSONObject blockSet = new JSONObject("{id:2, method: 'call', 'params':['database_api', 'get_ops_in_block', [" + hex + ",'false']]}");
  65.  
  66.                         webSocket.send(blockSet.toString());
  67.  
  68.                     } catch (JSONException ee) {
  69.                         ee.toString();
  70.                     }
  71.  
  72.                 }
  73.  
  74.  
  75.  
  76.             }
  77.  
  78.         } catch (JSONException e) {
  79.             e.printStackTrace();
  80.         }
  81.  
  82. //        Log.d("WS", data.toString());
  83. //        System.out.print(data);
  84.  
  85.     }
  86.  
  87.  
  88.     private int getNumberBlock(String text) {
  89.  
  90.         try {
  91.             JSONObject data = new JSONObject(text);
  92.             String method = data.getString("method");
  93.             //            Log.d("WS", method.toString());
  94.             JSONArray params = data.getJSONArray("params");
  95.  
  96.             if (Objects.equals(method, "notice") && params != null) {
  97.  
  98.                 JSONArray objectParam = params.getJSONArray(1);
  99.                 JSONObject arrayObjectParam = objectParam.getJSONObject(0);
  100.  
  101.                 String previousParam = arrayObjectParam.getString("previous");
  102.  
  103.                 Integer hex = Integer.parseInt(previousParam.substring(0, 8), 16);
  104.  
  105.                 return hex;
  106.             } else {
  107.                 return 0;
  108.             }
  109.         } catch (JSONException e) {
  110.             e.toString();
  111.         }
  112.  
  113.         return 0;
  114.  
  115.     }
  116.  
  117.     private void getContent(JSONObject data) {
  118.  
  119.         try{
  120.             JSONArray result = data.getJSONArray("result");
  121.  
  122.             for(int i = 0; result.length() >= i; i++){
  123.  
  124.                 JSONObject resJson = result.getJSONObject(i);
  125.  
  126.                 JSONArray resParam = resJson.getJSONArray("op");
  127.  
  128.                 String typeBlock = resParam.getString(0);
  129.  
  130.                 if(Objects.equals(typeBlock, "comment")){
  131.  
  132.                     JSONObject dataBlock = resParam.getJSONObject(1);
  133.  
  134.                     String parent_author = dataBlock.getString("parent_author");
  135.  
  136.                     if (Objects.equals(parent_author, "")) {
  137.  
  138.                         Log.d("WS", dataBlock.getString("permlink"));
  139.  
  140.                     }
  141.  
  142.                     Log.d("WS", dataBlock.toString());
  143.  
  144.                 }
  145.             }
  146.  
  147.         }catch(JSONException e){
  148.  
  149.         }
  150.  
  151.  
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement