From 04d883f1e26f0575bf558f5cb75725c708d0e73b Mon Sep 17 00:00:00 2001 From: Henry Varona Date: Sun, 27 Nov 2016 16:02:02 -0400 Subject: [PATCH] Get account name WS --- .../java/com/luminiasoft/bitshares/Test.java | 51 ++++++++++--------- .../bitshares/ws/GetAccountNameById.java | 12 ++++- .../bitshares/ws/GetAccountsByAddress.java | 16 ++++-- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/luminiasoft/bitshares/Test.java b/src/main/java/com/luminiasoft/bitshares/Test.java index bef0a8d..163e6c2 100644 --- a/src/main/java/com/luminiasoft/bitshares/Test.java +++ b/src/main/java/com/luminiasoft/bitshares/Test.java @@ -57,39 +57,44 @@ public class Test { } else if (response.result.getClass() == ArrayList.class) { List list = (List) response.result; if (list.size() > 0) { - if(list.get(0) instanceof AccountProperties){ + if (list.get(0) instanceof AccountProperties) { List accountPropertiesList = list; - for(AccountProperties accountProperties : accountPropertiesList){ - System.out.println("Account id: "+accountProperties.id); + for (AccountProperties accountProperties : accountPropertiesList) { + System.out.println("Account id: " + accountProperties.id); } - }else if (list.get(0) instanceof AssetAmount) { + } else if (list.get(0) instanceof AssetAmount) { AssetAmount assetAmount = (AssetAmount) list.get(0); System.out.println("Got fee"); System.out.println("amount: " + assetAmount.getAmount() + ", asset id: " + assetAmount.getAsset().getObjectId()); } else if (list.get(0).getClass() == ArrayList.class) { List sl = (List) list.get(0); if (sl.size() > 0) { - String accountId = (String) sl.get(0); - System.out.println("account id : " + accountId); - try { + if (response.result.getClass() == AccountProperties.class) { + AccountProperties accountProperties = (AccountProperties) response.result; + System.out.println("Got account properties " + accountProperties); + } else { + String accountId = (String) sl.get(0); + System.out.println("account id : " + accountId); + try { - // Create a custom SSL context. - SSLContext context = null; - context = NaiveSSLContext.getInstance("TLS"); - WebSocketFactory factory = new WebSocketFactory(); + // Create a custom SSL context. + SSLContext context = null; + context = NaiveSSLContext.getInstance("TLS"); + WebSocketFactory factory = new WebSocketFactory(); - // Set the custom SSL context. - factory.setSSLContext(context); + // Set the custom SSL context. + factory.setSSLContext(context); - WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL); - mWebSocket.addListener(new GetAccountNameById(accountId, null)); - mWebSocket.connect(); - } catch (IOException e) { - System.out.println("IOException. Msg: " + e.getMessage()); - } catch (WebSocketException e) { - System.out.println("WebSocketException. Msg: " + e.getMessage()); - } catch (NoSuchAlgorithmException ex) { - Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); + WebSocket mWebSocket = factory.createSocket(OPENLEDGER_WITNESS_URL); + mWebSocket.addListener(new GetAccountNameById(accountId, null)); + mWebSocket.connect(); + } catch (IOException e) { + System.out.println("IOException. Msg: " + e.getMessage()); + } catch (WebSocketException e) { + System.out.println("WebSocketException. Msg: " + e.getMessage()); + } catch (NoSuchAlgorithmException ex) { + Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); + } } } } @@ -632,7 +637,7 @@ public class Test { } } - public void testAccountNameById(){ + public void testAccountNameById() { try { // Create a custom SSL context. SSLContext context = null; diff --git a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java index 7a4db25..d2994a6 100644 --- a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java +++ b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountNameById.java @@ -39,7 +39,17 @@ public class GetAccountNameById extends WebSocketAdapter { public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList accountParams = new ArrayList(); ArrayList paramAddress = new ArrayList(); - paramAddress.add(accountID); + paramAddress.add(new JsonSerializable() { + @Override + public String toJsonString() { + return accountID; + } + + @Override + public JsonElement toJsonObject() { + return new JsonParser().parse(accountID); + } + }); accountParams.add(paramAddress); ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_ACCOUNTS, accountParams, RPC.VERSION, 1); websocket.sendText(getAccountByAddress.toJsonString()); diff --git a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java index 3ffadf3..9fc7dd3 100644 --- a/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java +++ b/src/main/java/com/luminiasoft/bitshares/ws/GetAccountsByAddress.java @@ -40,7 +40,17 @@ public class GetAccountsByAddress extends WebSocketAdapter { public void onConnected(WebSocket websocket, Map> headers) throws Exception { ArrayList accountParams = new ArrayList(); ArrayList paramAddress = new ArrayList(); - paramAddress.add(address.toString()); + paramAddress.add(new JsonSerializable() { + @Override + public String toJsonString() { + return address.toString(); + } + + @Override + public JsonElement toJsonObject() { + return new JsonParser().parse(address.toString()); + } + }); accountParams.add(paramAddress); ApiCall getAccountByAddress = new ApiCall(0, RPC.CALL_GET_KEY_REFERENCES, accountParams, RPC.VERSION, 1); websocket.sendText(getAccountByAddress.toJsonString()); @@ -52,8 +62,8 @@ public class GetAccountsByAddress extends WebSocketAdapter { String response = frame.getPayloadText(); Gson gson = new Gson(); - Type GetAccountByAddressResponse = new TypeToken>>(){}.getType(); - WitnessResponse>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); + Type GetAccountByAddressResponse = new TypeToken>>>(){}.getType(); + WitnessResponse>>> witnessResponse = gson.fromJson(response, GetAccountByAddressResponse); if (witnessResponse.error != null) { this.mListener.onError(witnessResponse.error);