1

I want to make a request in my android app when the button is clicked. In Python I could do this like that:

import requests
params = {
  'param1':some_string,
  'param2':some_int,
  'param3':another_string
  }
requests.post("https://some.api.com/method/some.method", params=params)

I'd like to do the same in Kotlin when I push the button. I tried tp do this with Fuel and khhtp but didn't succeed much -- app crashed as soon as I pushed the button, responsible for sending request.

UPD: What I used:

AndroidManifest.xml

...
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...

build.gradle

dependencies {
...
compile 'com.github.jkcclemens:khttp:0.1.0'
...
}

MainActivity.kt

fun request(){
    var message = "message"
    var uid = "123456" //I wanted to use it as int, but mapOf didn't allow me
    var token = "token"
    val payload = mapOf("token" to token, "user_id" to uid, "message" to message)
    get("https://some.api.com/method/some.method", params=payload)
    val popup = Toast.makeText(this,"Message sent!",Toast.LENGTH_LONG)
    popup.show()
}

activity_main.xml

<Button
...
    android:onClick="request" />

This is the example with khhtp, the one with Fuel is gone.

UPD2. Part of Logcat output:

enter image description here enter image description here

4 Answers4

0

You just need to look at the stack trace to find the issue. The code is throwing a NetworkOnMainThreadException. This happens when you try to access the network from within Android's main (often called UI) thread. This question have some good answers about this issue, however instead of trying to use AsyncTask make sure to read the documentation of your chosen network library and see how to make the call on a different thread.

0

I'm not sure if this is the root of your problem but your request method signature should be:

fun request(view: View)
{

}
Nelson Almendra
  • 784
  • 1
  • 9
  • 20
0

As other members answered your code is calling network operation on main thread that's why it is crashing . You can avoid this either by using Kotlin Coroutines or by using methods of Anko Library (Which is officially supported by kotlin to simplify things in android). Here i just give a reference for how to do Async call in Anko.

doAsync { 

    // Call all operation  related to network or other ui blocking operations here.
    uiThread { 
        // perform all ui related operation here    
    }
}

To do it as Kotlin Coroutines, you can refer this answer:-

Kotlin Coroutines the right way in Android

Suraj Nair
  • 1,729
  • 14
  • 14
0

I found the answer, basically what's happening is that you are not able to run internet connections at main thread, To override, add the following to the class where the user is performing network operations:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Reference (https://www.educative.io/edpresso/how-to-fix-androidosnetworkonmainthreadexception-error)

Carlos Montiel
  • 301
  • 1
  • 4
  • 16