嘗試使用 web3j 獲得平衡時出現異常
我使用上面的程式碼來使用 web3j 獲得平衡:
Web3j web3 = Web3j.build(new HttpService("https://ropsten.etherscan.io/address/0xAccountAddress")); EthGetBalance ethGetBalance = null; try { ethGetBalance = web3 .ethGetBalance("0xAccountAddress", DefaultBlockParameterName.LATEST) .sendAsync() .get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } BigInteger wei = ethGetBalance.getBalance();
發送請求後,我得到一個例外:
java.util.concurrent.ExecutionException:com.fasterxml.jackson.core.JsonParseException:意外字元(’<’(程式碼 60)):預期有效值(數字、字元串、數組、對象、’true’、‘false’或“空”)在
$$ Source: (okio.RealBufferedSource$1); line: 1, column: 2 $$ 在 java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) 在 java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) 在 backEnd.service.impl.BackerServiceImpl.getBalance(BackerServiceImpl.java:74 ) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl 的 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 的 backEnd.controller.BackerController.getBalance(BackerController.java:32)。 invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) at org.springframework .web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java: 877) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) 在 org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:974) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 在 org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:851) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在 org.apache.catalina。 core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java: 193) 在 org.springframework.web.filter.CorsFilter 的 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)。doFilterInternal(CorsFilter.java:96) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:158) 在 org.springframework.boot.actuate.metrics .web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:126) 在 org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:111) 在 org.springframework.web.filter.OncePerRequestFilter .doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina。core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal( HttpTraceFilter.java:84) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在 org.apache.catalina .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107 ) 在 org.apache.catalina.core.ApplicationFilterChain。internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) at org.springframework .web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java :166) 在 org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core 的 org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)。應用過濾鏈。internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) at org.springframework .web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java :166) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在 org.apache.catalina.authenticator 的 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)。 org.apache 上的 AuthenticatorBase.invoke(AuthenticatorBase.java:496)。catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java: 87) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) 在 org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66)在 org.apache.coyote.AbstractProtocolorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在 org.apache.coyote.AbstractProtocol 的 apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在 org.apache.coyote.AbstractProtocol 的 apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) $ ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1468) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java .util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在 java.lang.Thread.run(Thread.java:748) 有人可以建議我為什麼會收到此異常以及如何處理它?如果有任何幫助,我將不勝感激。
提前致謝!
要訪問乙太坊網路,您必須通過 API 詢問客戶端(例如 parity 或 geth)。API URL 在內部提供
Web3j.build(new HttpService())
。
https://ropsten.etherscan.io/address/0xAccountAddress
不是節點的 API: : 它是一個經典的網頁。您可以使用提供免費客戶端的Infura ,而不是執行自己的客戶端:這裡是初學者指南。