我有一个 http post 请求,但现在当尝试 运行 应用程序时,主 activity 在创建时崩溃

I have a http post request but now when trying to run the app the main activity crashes oncreate

我正在尝试获得一个 post 一个 json 和一个 header 到一个 api。但是当我 运行 程序在我的测试设备上创建时崩溃。

我曾尝试浏览论坛,但对 http3 中的什么错误导致此问题一无所知。

private val client = OkHttpClient()

@Throws(Exception::class)
fun fetchJson() {
    val formBody = FormBody.Builder()
        //json im trying to post

        .addEncoded("body", "{\n" +
                "  \"dateTime\": \"2018-06-21T09:18:23.283-07:00\",\n" +
                "  \"apiOptions\": [\n" +
                "    \"ALLOWPARTIALAUTH\"\n" +
                "  ],\n" +
                "  \"amount\": {\n" +
                "    \"cashback\": 20,\n" +
                "    \"surcharge\": 5,\n" +
                "    \"tax\": 15,\n" +
                "    \"tip\": 20,\n" +
                "    \"total\": 160\n" +
                "  },\n" +
                "  \"card\": {\n" +
                "    \"entryMode\": \"M\",\n" +
                "    \"expirationDate\": 1230,\n" +
                "    \"number\": \"4321000000001119\",\n" +
                "    \"present\": \"N\",\n" +
                "    \"securityCode\": {\n" +
                "      \"indicator\": \"1\",\n" +
                "      \"value\": \"333\"\n" +
                "    }\n" +
                "  },\n" +
                "  \"clerk\": {\n" +
                "    \"numericId\": 1576\n" +
                "  },\n" +
                "  \"customer\": {\n" +
                "    \"addressLine1\": \"65 Easy St\",\n" +
                "    \"firstName\": \"John\",\n" +
                "    \"lastName\": \"Smith\",\n" +
                "    \"postalCode\": \"65144\"\n" +
                "  },\n" +
                "  \"transaction\": {\n" +
                "    \"invoice\": \"192029\",\n" +
                "    \"notes\": \"Transaction notes are added here\",\n" +
                "    \"hotel\": {\n" +
                "      \"arrivalDateTime\": \"2018-06-18T15:39:01.594-07:00\",\n" +
                "      \"departureDateTime\": \"2018-06-21T09:18:23.283-07:00\",\n" +
                "      \"primaryChargeType\": 1,\n" +
                "      \"specialCode\": 1,\n" +
                "      \"additionalCharges\": {\n" +
                "        \"giftShop\": \"Y\",\n" +
                "        \"laundry\": \"Y\",\n" +
                "        \"miniBar\": \"Y\",\n" +
                "        \"other\": \"Y\",\n" +
                "        \"restaurant\": \"Y\",\n" +
                "        \"telephone\": \"Y\"\n" +
                "      },\n" +
                "      \"roomRates\": [\n" +
                "        {\n" +
                "          \"nights\": 2,\n" +
                "          \"rate\": 159.95\n" +
                "        },\n" +
                "        {\n" +
                "          \"nights\": 3,\n" +
                "          \"rate\": 125.38\n" +
                "        }\n" +
                "      ]\n" +
                "    },\n" +
                "    \"purchaseCard\": {\n" +
                "      \"customerReference\": \"D019D09309F2\",\n" +
                "      \"destinationPostalCode\": \"94719\",\n" +
                "      \"productDescriptors\": [\n" +
                "        \"Hamburger\",\n" +
                "        \"Fries\",\n" +
                "        \"Soda\",\n" +
                "        \"Cookie\"\n" +
                "      ]\n" +
                "    }\n" +
                "  },\n" +
                "  \"lighthouse\": {\n" +
                "    \"data\": \"eyJsaWdodGhvdXNlIjp7ImVtcGxveWVlaWQiOjEyMzQsImRldmljZWlkIjoiMTIzU0FCViJ9fQ==\"\n" +
                "  }\n" +
                "}")
        .build()
    val request = Request.Builder()
            //header information
        .url("https://utgapi.shift4test.com/api/rest/v1/transactions/sale")
        .addHeader("AccessToken","9EB227BC-A820-81CA-7607737B4809AA6E")
        .addHeader("CompanyName","PAWS")
        .addHeader("InterfaceName","ForwardPOS")
        .addHeader("InterfaceVersion","2.1")
        .post(formBody)
        .build()

    val response = client.newCall(request).execute()
    if (!response.isSuccessful) throw IOException("Unexpected code $response")

    System.out.println(response.body()?.string())
}

我希望程序能够 运行,所以它会与 api 对话,但它不会。

您不能在主线程上进行网络请求。将其包装在异步任务中。