我有一个http发布请求,但现在尝试运行应用程序时,主要活动在oncreate时崩溃

时间:2019-06-24 19:13:38

标签: android kotlin okhttp3

我试图获取一个带有api标头的json帖子。但是当我在测试设备上创建该程序时崩溃了。

我曾经尝试浏览过论坛,但没有发现来自http3的哪些错误导致了这种情况。

私有val客户端= 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通讯,但不会。

1 个答案:

答案 0 :(得分:0)

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