我有对api的POST请求,但崩溃了。我不确定如何让函数在后台而不是主线程中运行

时间:2019-07-08 17:38:50

标签: android-studio kotlin android-asynctask

我厌倦了使用异步,但是我不确定如何使用它。我所知道的是,该功能必须在后台线程上运行

        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());

    }

我环顾了四周,但是没有什么可以帮助我找到想要的东西。

1 个答案:

答案 0 :(得分:0)

您可以使用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());

    }
}