我有这个 POST 请求到 api 但它崩溃了。我不确定如何在后台而不是主线程中使用函数 运行

I have this POST request to an api but it crashes. Im not sure on how to have the function run in the background instead of the main thread

我厌倦了使用 Async 但我不确定如何使用它。我所知道的是该函数必须在后台 Thread

上 运行
        button.setOnClickListener {

            run()
        }
    }
}
     fun run() {

        val client = OkHttpClient()
// this sets up the header and body settings
        val mediaType = MediaType.parse("application/json")
        val body = RequestBody.create(
            mediaType,
            "{\r\n  \"dateTime\": \"2019-07-01T00:00:00-07:00\",\r\n  \"apiOptions\": [\r\n    \"ALLOWPARTIALAUTH\"\r\n  ],\r\n  \"amount\": {\r\n    \"cashback\": 20,\r\n    \"surcharge\": 5,\r\n    \"tax\": 15,\r\n    \"tip\": 20,\r\n    \"total\": 160\r\n  },\r\n  \"card\": {\r\n    \"entryMode\": \"M\",\r\n    \"expirationDate\": 1230,\r\n    \"number\": \"4321000000001119\",\r\n    \"present\": \"N\",\r\n    \"securityCode\": {\r\n      \"indicator\": \"1\",\r\n      \"value\": \"333\"\r\n    }\r\n  },\r\n  \"clerk\": {\r\n    \"numericId\": 1576\r\n  },\r\n  \"customer\": {\r\n    \"addressLine1\": \"65 Easy St\",\r\n    \"firstName\": \"John\",\r\n    \"lastName\": \"Smith\",\r\n    \"postalCode\": \"65144\"\r\n  },\r\n  \"transaction\": {\r\n    \"invoice\": \"192029\",\r\n    \"notes\": \"Transaction notes are added here\",\r\n    \"hotel\": {\r\n      \"arrivalDateTime\": \"2018-06-18T15:39:01.594-07:00\",\r\n      \"departureDateTime\": \"2018-06-21T09:18:23.283-07:00\",\r\n      \"primaryChargeType\": 1,\r\n      \"specialCode\": 1,\r\n      \"additionalCharges\": {\r\n        \"giftShop\": \"Y\",\r\n        \"laundry\": \"Y\",\r\n        \"miniBar\": \"Y\",\r\n        \"other\": \"Y\",\r\n        \"restaurant\": \"Y\",\r\n        \"telephone\": \"Y\"\r\n      },\r\n      \"roomRates\": [\r\n        {\r\n          \"nights\": 2,\r\n          \"rate\": 159.95\r\n        },\r\n        {\r\n          \"nights\": 3,\r\n          \"rate\": 125.38\r\n        }\r\n      ]\r\n    },\r\n    \"purchaseCard\": {\r\n      \"customerReference\": \"D019D09309F2\",\r\n      \"destinationPostalCode\": \"94719\",\r\n      \"productDescriptors\": [\r\n        \"Hamburger\",\r\n        \"Fries\",\r\n        \"Soda\",\r\n        \"Cookie\"\r\n      ]\r\n    }\r\n  },\r\n  \"lighthouse\": {\r\n    \"data\": \"eyJsaWdodGhvdXNlIjp7ImVtcGxveWVlaWQiOjEyMzQsImRldmljZWlkIjoiMTIzU0FCViJ9fQ==\"\r\n  }\r\n}"
        )
        // this creates the body and headder. The request builder is just talking to the api
        val request = Request.Builder()
            .url("https://utgapi.shift4test.com/api/rest/v1/transactions/sale")
            .post(body)
            .addHeader("AccessToken", "7CEECC0B-3D02-4497-8CDB-00F7545295CF")
            .addHeader("CompanyName", "PAWS")
            .addHeader("InterfaceName", "ForwardPOS")
            .addHeader("InterfaceVersion", "2.1")
            .addHeader("Content-Type", "application/json")
            .addHeader("User-Agent", "PostmanRuntime/7.15.0")
            .addHeader("Accept", "*/*")
            .addHeader("Cache-Control", "no-cache")
            .addHeader("Postman-Token", "a3482f14-91c4-4300-8ad4-748c3c0a8107,2f922be0-d117-44fc-b13f-c975eb40098e")
            .addHeader("Host", "utgapi.shift4test.com")
            .addHeader("accept-encoding", "gzip, deflate")
            .addHeader("content-length", "1611")
            .addHeader("Connection", "keep-alive")
            .addHeader("cache-control", "no-cache")
            .build()

        val response = client.newCall(request).execute();
        if (!response.isSuccessful())
//prints out the json
            System.out.println(response.body()?.string());

    }

我环顾四周,但没有任何东西可以帮助我找到我要找的东西。

您可以使用 Kotlin 协程在后台线程上 运行 任务。

首先确保你的应用级别build.gradle文件中有这个依赖项:

dependencies {
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.2'
}

这段代码将在后台执行:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            GlobalScope.launch{
                run()
            }
        }
    }

    suspend fun run()= withContext(Dispatchers.IO) {

        val client = OkHttpClient()
        // this sets up the header and body settings
        val mediaType = MediaType.parse("application/json")
        val body = RequestBody.create(
            mediaType,
            "{\r\n  \"dateTime\": \"2019-07-01T00:00:00-07:00\",\r\n  \"apiOptions\": [\r\n    \"ALLOWPARTIALAUTH\"\r\n  ],\r\n  \"amount\": {\r\n    \"cashback\": 20,\r\n    \"surcharge\": 5,\r\n    \"tax\": 15,\r\n    \"tip\": 20,\r\n    \"total\": 160\r\n  },\r\n  \"card\": {\r\n    \"entryMode\": \"M\",\r\n    \"expirationDate\": 1230,\r\n    \"number\": \"4321000000001119\",\r\n    \"present\": \"N\",\r\n    \"securityCode\": {\r\n      \"indicator\": \"1\",\r\n      \"value\": \"333\"\r\n    }\r\n  },\r\n  \"clerk\": {\r\n    \"numericId\": 1576\r\n  },\r\n  \"customer\": {\r\n    \"addressLine1\": \"65 Easy St\",\r\n    \"firstName\": \"John\",\r\n    \"lastName\": \"Smith\",\r\n    \"postalCode\": \"65144\"\r\n  },\r\n  \"transaction\": {\r\n    \"invoice\": \"192029\",\r\n    \"notes\": \"Transaction notes are added here\",\r\n    \"hotel\": {\r\n      \"arrivalDateTime\": \"2018-06-18T15:39:01.594-07:00\",\r\n      \"departureDateTime\": \"2018-06-21T09:18:23.283-07:00\",\r\n      \"primaryChargeType\": 1,\r\n      \"specialCode\": 1,\r\n      \"additionalCharges\": {\r\n        \"giftShop\": \"Y\",\r\n        \"laundry\": \"Y\",\r\n        \"miniBar\": \"Y\",\r\n        \"other\": \"Y\",\r\n        \"restaurant\": \"Y\",\r\n        \"telephone\": \"Y\"\r\n      },\r\n      \"roomRates\": [\r\n        {\r\n          \"nights\": 2,\r\n          \"rate\": 159.95\r\n        },\r\n        {\r\n          \"nights\": 3,\r\n          \"rate\": 125.38\r\n        }\r\n      ]\r\n    },\r\n    \"purchaseCard\": {\r\n      \"customerReference\": \"D019D09309F2\",\r\n      \"destinationPostalCode\": \"94719\",\r\n      \"productDescriptors\": [\r\n        \"Hamburger\",\r\n        \"Fries\",\r\n        \"Soda\",\r\n        \"Cookie\"\r\n      ]\r\n    }\r\n  },\r\n  \"lighthouse\": {\r\n    \"data\": \"eyJsaWdodGhvdXNlIjp7ImVtcGxveWVlaWQiOjEyMzQsImRldmljZWlkIjoiMTIzU0FCViJ9fQ==\"\r\n  }\r\n}"
        )
        // this creates the body and headder. The request builder is just talking to the api
        val request = Request.Builder()
            .url("https://utgapi.shift4test.com/api/rest/v1/transactions/sale")
            .post(body)
            .addHeader("AccessToken", "7CEECC0B-3D02-4497-8CDB-00F7545295CF")
            .addHeader("CompanyName", "PAWS")
            .addHeader("InterfaceName", "ForwardPOS")
            .addHeader("InterfaceVersion", "2.1")
            .addHeader("Content-Type", "application/json")
            .addHeader("User-Agent", "PostmanRuntime/7.15.0")
            .addHeader("Accept", "*/*")
            .addHeader("Cache-Control", "no-cache")
            .addHeader("Postman-Token", "a3482f14-91c4-4300-8ad4-748c3c0a8107,2f922be0-d117-44fc-b13f-c975eb40098e")
            .addHeader("Host", "utgapi.shift4test.com")
            .addHeader("accept-encoding", "gzip, deflate")
            .addHeader("content-length", "1611")
            .addHeader("Connection", "keep-alive")
            .addHeader("cache-control", "no-cache")
            .build()

        val response = client.newCall(request).execute();
        if (!response.isSuccessful())
        //prints out the json
            System.out.println(response.body()?.string());

    }
}