Web3j

獲取 ethBalance 時出現異常

  • November 16, 2017

web3 = Web3j.build(new HttpService());

        EthGetBalance ethGetBalance = web3.ethGetBalance("0xb34a5b6a39bceafeb8e23c2cc526889d85bf664e", DefaultBlockParameterName.LATEST).send();
        BigInteger balance = ethGetBalance.getBalance();
        System.out.println("balance - "+balance);

java.net.ConnectException:無法在 okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:225) 處連接到 localhost/0:0:0:0:0:0:0:1:8545 在 okhttp3.internal .connection.RealConnection.connect(RealConnection.java:149) 在 okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:195) 在 okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) 在 okhttp3。 internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) 在 okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3 .internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 在 okhttp3.internal.cache.CacheInterceptor。攔截(CacheInterceptor.java:93)在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)在 okhttp3.internal.http.BridgeInterceptor .intercept(BridgeInterceptor.java:93) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) 在 okhttp3.internal.http。 RealInterceptorChain.proceed(RealInterceptorChain.java:92) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 在 okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211) 在 okhttp3.internal.http.RealInterceptorChain .proceed(RealInterceptorChain.java:92) 在 okhttp3。internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) at okhttp3.RealCall.execute(RealCall.java:69) at org.web3j.protocol.http.HttpService .performIO(HttpService.java:106) 在 org.web3j.protocol.Service.send(Service.java:30) 在 org.web3j.protocol.core.Request.send(Request.java:68) 在 com.mobiloitte。 coinbase.TestEthreum.createConnection(TestEthreum.java:56) at com.mobiloitte.coinbase.TestEthreum.main(TestEthreum.java:74) 原因:java.net.ConnectException:連接被拒絕:在 java.net.DualStackPlainSocketImpl.waitForConnect 連接(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl。connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect( Unknown Source) at okhttp3.internal.platform.Platform.connectSocket(Platform.java:124) at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223) … 25 更多還有 25 個還有 25 個

您應該在本地主機上執行節點(例如 geth)並告訴它打開 rpc 埠

geth --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpcapi="db,eth,net,web3,personal,debug" --rpccorsdomain "*"

小心,使用 –rpccorsdomain “*” 是一個安全問題

引用自:https://ethereum.stackexchange.com/questions/30908