A
A
art_guzev2020-06-01 17:06:26
Android
art_guzev, 2020-06-01 17:06:26

How to send a simple post request in a Kotlin class?

Firebase cloud messaging is connected in the application. There is a kotlin class that displays the device token.

package com.placeholder

import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import android.util.Log.d as d1

class MyFirebaseMessagingService : FirebaseMessagingService(){
    override fun onNewToken(token: String) {
        d1("TAG", "The token refreshed: $token")
    }
}


How to make a POST request like https://mydomain.com/post/?token=(device token $token) in the same class ?
Volley connected.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Ommonik, 2020-06-03
@art_guzev

Specifically, in the class - xs, but Postman has a code generator for requests (though in Java)
times
Unirest.setTimeouts(0, 0);
HttpResponse response = Unirest.post(" http://host.ru:80/user/register ")
.header("Content-Type", "text/plain")
.body("{\n \"firstName\" : \"Conso\",\n \"insertToTestClient\": false,\n \"investEmailDomain\": true,\n \"isLegal\": false,\n \"lastName\": \"Barsukov\" ,\n \"middleName\": \"Medoedovich\"\n}")
.asString();
two
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("
RequestBody body = RequestBody.create(mediaType, "{\n \"firstName\": \"Conso\",\n \"insertToTestClient\": false,\n \"investEmailDomain\": true,\n \"isLegal \": false,\n \"lastName\": \"Barsukov\",\n \"middleName\": \"Medoedovich\"\n}");
Request request = new Request.Builder()
.url(" http://host.ru:80/user/register ")
.method("POST", body)
.addHeader("Content-Type", "text/plain ")
.build();
Response response = client.newCall(request).execute();
just plug it in where you want it and it works. and there it is possible to spread the variables and wrap them as needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question