K
K
korner-brazers2021-04-27 08:58:17
Android
korner-brazers, 2021-04-27 08:58:17

Android how to send a post request?

Hello everyone, I'm trying to send a post request to the server using android. 1000 options and only a few manage to launch.

I found the Fuel library, supposedly the best of the best)) Here is a small example of their code.

Fuel.post("https://httpbin.org/post")
    .jsonBody("{ \"foo\" : \"bar\" }")
    .also { println(it) }
    .response { result -> }


Made a test file in php

<?
print_r($_POST);
?>


The response returns an empty Array()

Already tried all options:

.jsonBody("{ \"foo\" : \"bar\" }")
 .jsonBody("foo=bar")
 .body("{ \"foo\" : \"bar\" }")
 .body("foo=bar")


Nothing helps, I thought, okay, to hell with it, let's do it somehow by means of android.

fun sendPostRequest(userName:String, password:String) {

        var reqParam = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(userName, "UTF-8")
        reqParam += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8")
        val mURL = URL("http://.../test.php")

        with(mURL.openConnection() as HttpURLConnection) {
            // optional default is GET
            requestMethod = "POST"

            val wr = OutputStreamWriter(getOutputStream());
            wr.write(reqParam);
            wr.flush();

            Log.i(App.TAG,"URL : $url")
            Log.i(App.TAG,"Response Code : $responseCode")

            BufferedReader(InputStreamReader(inputStream)).use {
                val response = StringBuffer()

                var inputLine = it.readLine()
                while (inputLine != null) {
                    response.append(inputLine)
                    inputLine = it.readLine()
                }
                Log.i(App.TAG,"Response : $response")
            }

            wr.close()
        }
    }


The code works, the request sends as it should, but there is one big BUT! The code is synchronous, at the time of sending everything hangs, I tried to make it asynchronous, but it seems that it’s not fate to do it on Kotlin, I did not find a working example of how to do it.

Libraries that I use

dependencies {
    implementation 'androidx.leanback:leanback:1.0.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.work:work-runtime-ktx:2.5.0'
    implementation 'androidx.tvprovider:tvprovider:1.0.0'
    implementation 'androidx.preference:preference-ktx:1.1.1'


    //Update
    implementation 'com.g00fy2:versioncompare:1.2.8'

    //Network
    implementation "com.google.code.gson:gson:2.8.6"
    implementation 'com.github.kittinunf.fuel:fuel:2.3.1'
    implementation 'com.github.kittinunf.fuel:fuel-gson:2.3.1'
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2021-04-27
@Jacen11

In fact, everyone uses retrofit.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question