运行程序时POST API请求崩溃

时间:2019-06-25 16:57:53

标签: android-studio kotlin crash

每当我启动程序并进入包含发布请求的类时,整个应用程序将关闭并崩溃

我四处张望,但我的类似情况没有解决。由于我是Kotlin的新手,所以我对代码的了解不多。

private val client = OkHttpClient()

    @Throws(Exception::class)
    fun run() {
        val formBody = FormBody.Builder()
            //json im trying to post
//error message is angering me
            .add("name","fillerforbody" )
            .build() as RequestBody
        val request = Request.Builder()
            //header information then post body
            .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收到的内容的println响应。

0 个答案:

没有答案