title: Passing android.content.Context to a suspended function causes android.os.NetworkOnMainThreadException
body: <p>I have a suspended function that makes a network call to get some data and returns it. I am using Volley.</p>
  <pre><code>class PokeClient {
  suspend fun getPokemonData(pokemonName: String, context: Context) = suspendCoroutine&lt;Pokemon&gt; { cont -&gt;
      val queue = Volley.newRequestQueue(context)
      val request = url.plus(pokemonName)
      // some skipped code here
  }
  </code></pre>
  <p>I am calling it in my activity like this:</p>
  <pre><code>GlobalScope.launch {
          val pokeResult = pokeClient.getPokemonData(nameToSearch, applicationContext)
          pokemonList.add(pokeResult)
          pokemonAdapter.notifyItemInserted(pokemonList.size - 1)
      }
  </code></pre>
  <p>And I get an error like: <strong>android.os.NetworkOnMainThreadException</strong></p>
  <p>I suspect it may be because I am passing the context from the MainActivity and that is not correct. What is the alternative here?</p>
  <p>Thanks,</p>
  <p>Android Newbie</p>
  <p>UPDATE:</p>
  <p><a href="https://i.stack.imgur.com/q4mjl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q4mjl.png" alt="enter image description here" /></a></p>
  <pre><code>class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
  
      val pokeClient = PokeClient()
      var pokemonList = mutableListOf&lt;Pokemon&gt;()
  
      val pokemonAdapter = PokemonRvAdapter(pokemonList)
      val pokemonRv = findViewById&lt;RecyclerView&gt;(R.id.rv_pokemon)
      pokemonRv.adapter = pokemonAdapter
      pokemonRv.layoutManager = LinearLayoutManager(this)
  
      val searchButton = findViewById&lt;Button&gt;(R.id.search_btn)
      searchButton.setOnClickListener {
          val toast = Toast.makeText(applicationContext, &quot;Loading poke data&quot;, Toast.LENGTH_SHORT)
          toast.show()
  
          val nameToSearch = findViewById&lt;EditText&gt;(R.id.search_pokemon_txt).text.toString().lowercase()
  
          GlobalScope.launch(Dispatchers.IO) {
              val pokeResult = pokeClient.getPokemonData(nameToSearch, applicationContext)
              pokemonList.add(pokeResult)
              pokemonAdapter.notifyItemInserted(pokemonList.size - 1)
          }
      }
  }
  }
  </code></pre>
  <p>PokeClient:</p>
  <pre><code>const val url = &quot;https://pokeapi.co/api/v2/pokemon/&quot;
  
  class PokeClient {
  suspend fun getPokemonData(pokemonName: String, context: Context) = suspendCoroutine&lt;Pokemon&gt; { cont -&gt;
      val queue = Volley.newRequestQueue(context)
      val request = url.plus(pokemonName)
  
          val stringRequest = StringRequest(Request.Method.GET, request, Response.Listener&lt;String&gt; { response -&gt;
              val jObj = JSONObject(response)
              val imgUrl = jObj
                  .getJSONObject(&quot;sprites&quot;)
                  .getJSONObject(&quot;other&quot;)
                  .getJSONObject(&quot;official-artwork&quot;)
                  .getString(&quot;front_default&quot;)
  
              val inputStream = URL(imgUrl).openStream()
              /* call continuation.resume and pass your object */
              cont.resume(Pokemon(name = jObj.getString(&quot;name&quot;), image = BitmapFactory.decodeStream(inputStream)))
          }, Response.ErrorListener { err -&gt;
              cont.resumeWithException(err)
          })
  
          queue.add(stringRequest)
  }
  }
  </code></pre>