Swift HockeyApp:自定义崩溃日志未显示在控制台上

时间:2019-04-25 07:25:32

标签: swift hockeyapp

我正在探索将崩溃日志上传到HockeyApp,但是崩溃日志没有显示在HockeyApp的控制台面板上。

崩溃日志的格式照这样从他们的演示代码复制到他们的website上,并修改了PackageCrashReporter 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.access$2400(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)
  ...

post建议date字段可能是错误的,但是我已经根据演示代码进行了调整,因此我认为应该没问题。

post还建议检查版本字段。 HockeyApp上可用的版本如下所示,我的目标版本为1.3: enter image description here

我尝试了1.31.3(3)3,但似乎都无法正常工作。

我的POST请求返回创建的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()

}

1 个答案:

答案 0 :(得分:0)

联系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
}