-1

I'm trying to execute an OPTIONS request on Android Studio (SDK 19) using the okHttp library. Couldn't see too many examples on how to call an OPTIONS request using this library, so I checked the documentation and this is what I've achieved. Here's my code:

RequestBody requestBody = RequestBody.create(null, new byte[0]);

    OkHttpClient client = new OkHttpClient();
    URL url = null;
    try {
        url = new URL("SOME_URL");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    Request.Builder formBody = new Request.Builder().url(url).method("OPTIONS", null).header("Content-Length", "0");
    Response response;
    Call call = client.newCall(formBody.build());
    try {
        response = call.execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

The application crashes on the execution but I can't see why. I'm guessing there's something wrong with my request.

response = call.execute();

Here's my stack trace:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fsck.k9.debug/com.fsck.k9.SimpleHttpSend}: android.os.NetworkOnMainThreadException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: 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 com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
    at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
    at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
    at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
    at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
    at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327)
    at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
    at com.squareup.okhttp.Call.getResponse(Call.java:267)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
    at com.squareup.okhttp.Call.execute(Call.java:79)
    at com.fsck.k9.ActiveSyncClient.OptionsRequest(ActiveSyncClient.java:36)
    at com.fsck.k9.HttpSendVM.<init>(HttpSendVM.java:13)
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at com.fsck.k9.SimpleHttpSend.onCreate(SimpleHttpSend.java:20)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Hawks
  • 1
  • 1

1 Answers1

1

The error is android.os.NetworkOnMainThreadException. See the various answer on this in stackoverflow.

How to fix 'android.os.NetworkOnMainThreadException'?

Android Okhttp asynchronous calls

Yuri Schimke
  • 10,475
  • 3
  • 30
  • 60