我正在制作一个Android应用程序,该应用程序将具有使用YouTrack REST API的错误报告模块
GET工作正常-我现在可以毫无问题地获取所有数据(如获取所有问题)
我正在努力的是POST方法。 根据{{3}},这是添加新问题的示例请求:
curl -X POST \
'http://<url>/api/issues?fields=reporter%28login,name%29,idReadable,id,project%28id,name,leader%28login,name%29%29,summary,description' \
-H 'Authorization: Bearer xyz' \
-H 'Accept: application/json' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{
"project":{"id":"0-0"},
"summary":"REST API lets you create issues!",
"description":"Let'\''s create a new issue using YouTrack'\''s REST API."
}'
当我复制以上内容时,一切正常,一切正常,并将问题正确地添加到您的轨道上
当我尝试从Kotlin做同样的事情时,问题就开始了。我尝试了以下方法:
val request = "api/issues?fields=reporter%28login,name%29,idReadable,id,project%28id,name,leader%28login,name%29%29,summary,description"
val url = URL("<url>$request")
val connection = url.openConnection() as? HttpURLConnection
connection?.requestMethod = "POST"
connection?.addRequestProperty("Authorization","Bearer xyz")
connection?.addRequestProperty("Accept","application/json")
connection?.addRequestProperty("Cache-Control","no-cache")
connection?.addRequestProperty("Content-Type","application/json")
connection?.useCaches = false
connection?.doInput = true
connection?.doOutput = true
val data = ArrayList<String>()
data.add("\"project\":{\"id\":\"0-0\"}")
data.add("\"summary\":\"REST API lets you create issues\"")
data.add("\"description\":\"New Issue\"")
val result = data.joinToString(separator = ",\n").toByteArray(Charsets.UTF_8)
connection?.outputStream?.write(result)
connection?.outputStream?.close()
connection?.connect()
val responceCode = connection?.responseCode
if(responceCode != null && responceCode < 400)
Log.d("FP-YT", "Responce code: $responceCode")
else{
//responceCode = 500
val errors = connection?.errorStream?.read() //errors is "60"
val message = connection?.responseMessage //message is "Request failed."
Log.e("FP-YT", "Responce code: $responceCode:,\n$errors\n$message")
}
val inputStream = connection?.inputStream //here is FileNotFoundException
val reader = InputStreamReader(inputStream)
val br = BufferedReader(reader)
return br.readText()
如上面的代码所述,它失败并显示http错误500(errorStream仅包含数字60
),responseMessage同样具有描述性Request failed.
。
在任何人问之前,协议就是http。