1

I've used web3.js to handle my contracts, and it worked very well.

But web3j is pretty hard for me since the beginning.

I'm using android studio, infura, web3j. Since I don't know very much about web3j, I can't explain more.

Following is my code:

Web3j web3 = Web3jFactory.build(new HttpService("http://kovan.infura.io/v3/4eae1227d0544298b86b29de0b5cf048"));
    Log.d("MYDEBUG | web3","web3 build complete");

    Web3ClientVersion web3ClientVersion = null;

    try {
        web3ClientVersion = web3.web3ClientVersion().send();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    catch(NetworkOnMainThreadException e){
        e.printStackTrace();
    }

    if(web3ClientVersion != null) {
        String web3ClientVersionString = web3ClientVersion.getWeb3ClientVersion();
        if (web3ClientVersionString == null) {
            Log.d("MYDEBUG | web3j version ", "NULL");
        } else {
            Log.d("MYDEBUG | web3j version", web3ClientVersionString);
        }
    }
    else{
        Log.d("MYDEBUG | web3ClientVersion ", "NULL");
    }

Since the very beginning of the project, building web3j fails.

When I run this code, web3ClientVersion always returns null. I can't figure out what went wrong. http://kovan.infura.io/v3/4eae1227d0544298b86b29de0b5cf048 is my infura project endpoint.

The error is below.

W/System.err: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90) at java.net.InetAddress.getAllByName(InetAddress.java:787) at okhttp3.Dns$1.lookup(Dns.java:39) at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:171) at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:137) at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:82) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:171) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at 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:110) at org.web3j.protocol.Service.send(Service.java:32) at org.web3j.protocol.core.Request.send(Request.java:72) at com.example.myapplication.MainActivity.getBalance(MainActivity.java:133) at com.example.myapplication.MainActivity$2.onClick(MainActivity.java:82) at android.view.View.performClick(View.java:6897) at android.widget.TextView.performClick(TextView.java:12693) at android.view.View$PerformClick.run(View.java:26104) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6944) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

  • @Ismael you meant https instead of http:D – Div Jun 12 at 12:01
  • @Div You are correct. Try with https instead of http I think secure connection are required now. – Ismael Jun 12 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.