Swift HockeyApp:控制台上未显示自定义崩溃日志
Swift HockeyApp: Custom crash log not shown on console
我正在探索将崩溃日志上传到 HockeyApp,但崩溃日志没有显示在 HockeyApp 的控制台面板上。
崩溃日志的格式是从他们 website 上的演示代码复制而来的,修改为 Package
和 CrashReporter Key
:
Package: //Bundle ID as shown on HockeyApp console
Version: 1.3
OS: Mac OS X 10.14.2 (18C54)
Manufacturer: Apple
Model: MacBookPro12,1
Date: Wed Apr 24 15:35:08 GMT+08:00 2019
CrashReporter Key: //Incident key generated in the crash log
java.lang.RuntimeException: Unable to start activity ComponentInfo\{de.codenauts.hockeyapp/de.codenauts.hockeyapp.MainActivity\}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4066)
at android.app.ActivityThread.access00(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2140)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
...
这个提示date
字段可能有误,不过我在demo代码中做了调整,应该没问题。
这 还建议检查版本字段。 HockeyApp 上可用的版本如下所示,我的目标版本是 1.3:
我已经尝试了 1.3
、1.3(3)
、3
,但似乎都不起作用。
我的POST请求returns创建了一个statusCode
的201,所以我想网络请求是成功的。我错过了什么吗?
编辑:POST 函数
这是我用来上传自定义崩溃报告的 POST 函数。
func postLog(filename: String) {
let urlString = "https://rink.hockeyapp.net/api/2/apps/APP_ID/crashes/upload"
guard let url = URL(string: urlString) else {return}
guard let filepath = Bundle.main.url(forResource: filename, withExtension: nil) else {
print("Filepath nil")
return
}
let fileData = NSData(contentsOf: filepath)
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpBody = createBody(parameters: [:], boundary: boundary, data: fileData! as Data, mimeType: "text/plain", filename: filename)
let dataTask = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error.localizedDescription)
return
}
if let response = response {
print(response)
}
}
dataTask.resume()
}
联系 HockeyApp 支持后,问题出在 createBody
函数。
name
参数需要log
而不是file
。
func createBody(parameters: [String: String],
boundary: String,
data: Data,
mimeType: String,
filename: String) -> Data {
let body = NSMutableData()
let boundaryPrefix = "--\(boundary)\r\n"
for (key, value) in parameters {
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
body.appendString(boundaryPrefix)
//Problem was on this line, previously it was name=\"file\". After changing to log, it worked
body.appendString("Content-Disposition: form-data; name=\"log\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimeType)\r\n\r\n")
body.append(data)
body.appendString("\r\n")
body.appendString("--".appending(boundary.appending("--")))
return body as Data
}
我正在探索将崩溃日志上传到 HockeyApp,但崩溃日志没有显示在 HockeyApp 的控制台面板上。
崩溃日志的格式是从他们 website 上的演示代码复制而来的,修改为 Package
和 CrashReporter Key
:
Package: //Bundle ID as shown on HockeyApp console
Version: 1.3
OS: Mac OS X 10.14.2 (18C54)
Manufacturer: Apple
Model: MacBookPro12,1
Date: Wed Apr 24 15:35:08 GMT+08:00 2019
CrashReporter Key: //Incident key generated in the crash log
java.lang.RuntimeException: Unable to start activity ComponentInfo\{de.codenauts.hockeyapp/de.codenauts.hockeyapp.MainActivity\}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4066)
at android.app.ActivityThread.access00(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2140)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
...
这个date
字段可能有误,不过我在demo代码中做了调整,应该没问题。
这
我已经尝试了 1.3
、1.3(3)
、3
,但似乎都不起作用。
我的POST请求returns创建了一个statusCode
的201,所以我想网络请求是成功的。我错过了什么吗?
编辑:POST 函数
这是我用来上传自定义崩溃报告的 POST 函数。
func postLog(filename: String) {
let urlString = "https://rink.hockeyapp.net/api/2/apps/APP_ID/crashes/upload"
guard let url = URL(string: urlString) else {return}
guard let filepath = Bundle.main.url(forResource: filename, withExtension: nil) else {
print("Filepath nil")
return
}
let fileData = NSData(contentsOf: filepath)
var request = URLRequest(url: url)
request.httpMethod = "POST"
let boundary = "Boundary-\(UUID().uuidString)"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpBody = createBody(parameters: [:], boundary: boundary, data: fileData! as Data, mimeType: "text/plain", filename: filename)
let dataTask = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error.localizedDescription)
return
}
if let response = response {
print(response)
}
}
dataTask.resume()
}
联系 HockeyApp 支持后,问题出在 createBody
函数。
name
参数需要log
而不是file
。
func createBody(parameters: [String: String],
boundary: String,
data: Data,
mimeType: String,
filename: String) -> Data {
let body = NSMutableData()
let boundaryPrefix = "--\(boundary)\r\n"
for (key, value) in parameters {
body.appendString(boundaryPrefix)
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
body.appendString(boundaryPrefix)
//Problem was on this line, previously it was name=\"file\". After changing to log, it worked
body.appendString("Content-Disposition: form-data; name=\"log\"; filename=\"\(filename)\"\r\n")
body.appendString("Content-Type: \(mimeType)\r\n\r\n")
body.append(data)
body.appendString("\r\n")
body.appendString("--".appending(boundary.appending("--")))
return body as Data
}