title: NetworkOnMainThreadException using rxjava android
body: <p>I'm using rxjava2 to do a network call and I am getting the dreaded. NetworkOnMainThreadException.  I am using fromCallable, which should defer the call until subscription.  Here is my code: </p>
  
  <pre><code>override fun getMovie(movieId: Int) {
      Single.fromCallable{getMovieFromAPI(movieId)}
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .doOnError { throwable -&gt; Log.e(TAG, throwable.message) }
              .unsubscribeOn(Schedulers.io())
              .subscribe(object : SingleObserver&lt;ArrayList&lt;Movie&gt;&gt; {
                  override fun onSuccess(t: ArrayList&lt;Movie&gt;) {
                      view?.get()?.setMoviesActivityUIElements(t)
                  }
  
                  override fun onSubscribe(d: Disposable) {
                      compositeDisposable.add(d)
                  }
  
                  override fun onError(e: Throwable) {
                      Log.e(TAG,e.localizedMessage)
                      e.printStackTrace()
  
                  }
  
              })
  }
  </code></pre>
  
  <p>and my getMovieFromAPI(movieID): </p>
  
  <pre><code>fun getMovieFromAPI(movieId : Int) : ArrayList&lt;Movie&gt; {
  
      val URL = Constants.API_URL + "/" + movieId
      val httpURLConnection: HttpURLConnection
      var movieList: ArrayList&lt;Movie&gt; = ArrayList()
  
      val requestParam = RequestParam(Constants.GET_METHOD, URL)
      requestParam.addParam(Constants.PARAM_LANGUAGE, Constants.PARAM_ENGLISH)
      requestParam.addParam(Constants.PARAM_API_KEY, Constants.APIKey)
  
      try {
          httpURLConnection = requestParam.setUpConnection()
          val statusCode = httpURLConnection.responseCode // &lt;--error happens here
          val stringBuilder = Movie.getResponse(httpURLConnection)
          when (statusCode) {
              HttpURLConnection.HTTP_OK -&gt; movieList = Movie.parseMovie(stringBuilder.toString())
  
              HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_UNAUTHORIZED -&gt; {
                  view?.get()?.showError(statusCode)
                  errorMessage = Movie.errorMessage
                  movieList.clear()
              }
              else -&gt; {
                  view?.get()?.showError(-1)
                  movieList.clear()
              }
          }
          httpURLConnection.disconnect()
  
  
      } catch (e: IOException) {
          e.printStackTrace()
      } catch (e: JSONException) {
          e.printStackTrace()
      }
  
      return movieList
  }
  </code></pre>
  
  <p>Why is this happening?  I looked at <a href="https://stackoverflow.com/questions/34349334/rxjava-and-retrofit2-networkonmainthreadexception">this question</a> and it doesn't answer my question. </p>
  
  <p><strong>EDIT</strong>
  Adding Log.e(TAG,Thread.currentThread().toString()) in the first line of getMovieFromAPI, </p>
  
  <p>prints: Thread[main,5,main]</p>
  
  <pre><code>07-06 06:49:55.882 1897-1897/com.webnation.imdb E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.webnation.imdb, PID: 1897
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.webnation.imdb/com.webnation.imdb.MovieDetailActivity}: android.os.NetworkOnMainThreadException
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
      at android.app.ActivityThread.-wrap11(Unknown Source:0)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
      at android.os.Handler.dispatchMessage(Handler.java:106)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
   Caused by: android.os.NetworkOnMainThreadException
      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
      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.android.okhttp.Dns$1.lookup(Dns.java:39)
      at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
      at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
      at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
      at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
      at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
      at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
      at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
      at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
      at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
      at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
      at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(Unknown Source:0)
      at com.webnation.imdb.presenter.MovieDetailPresenter.getMovieFromAPI(MovieDetailPresenter.kt:76)
      at com.webnation.imdb.MovieDetailActivity.onCreate(MovieDetailActivity.kt:73)
      at android.app.Activity.performCreate(Activity.java:7009)
      at android.app.Activity.performCreate(Activity.java:7000)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
  </code></pre>