android - 我有一个 http 发布请求,但现在在尝试运行应用程序时,主要活动在创建时崩溃
问题描述
我试图发布一个带有 api 标题的 json。但是当我在我的测试设备上创建崩溃时运行程序时。
我试图环顾论坛,但没有关于 http3 的哪些错误导致此问题。
私人验证客户端 = 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 对话,但它不会。
解决方案
您不能在主线程上发出网络请求。将其包装在异步任务中。
推荐阅读
- python - 模块“保管箱”没有属性“客户端”
- c++ - 为什么编写的代码在执行时出现溢出错误
- ms-access - 如何在 Access VBA 中对包含 # 特殊字符的数据使用 Like?
- haskell - 在haskell中不带括号打印
- php - MYSQL 查询总和
- java - 动态网络项目
- perl - 是否可以在 Selenium 和 LWP::UserAgent 之间共享会话?
- java - 访问B类中的A类字段时Java同步块
- react-native - React Native 中 componentDidUpdate 中的 Animated.timing
- sql - 如何不扫描所有记录以按复杂条件查询顶行