iOS应用无法将json文件发送到Flask API

时间:2019-04-07 14:56:27

标签: swift api flask

我有一个收集数据并将其发送到我的烧瓶服务器的应用程序。我能够成功建立连接,但是无法将数据保存到本地计算机中。参见下面的代码:

iOS端:

@IBAction func detectSteps(sender: UIButton) {

    print(data_dict)
    guard let jsonData = try? JSONSerialization.data(withJSONObject: data_dict, options:[.prettyPrinted]) else{ return }
    print("My jsonData")
    //below two lines check if the json was converted properly or not
    if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
        print(JSONString)
    }
    guard let url = URL(string: "http://10.105.86.98:8000/dataCollect") else {
        print("Error: cannot create URL")
        return}
    print(url)
    var request = URLRequest(url: url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Context-Type")
    request.httpBody = jsonData

    let session = URLSession.shared

    let task = session.dataTask(with: request, completionHandler:{ _, _, _ in })
    task.resume()
}

烧瓶侧面:

data = jsonify(request.get_json(force = True))

with open(data, 'w', encoding = 'utf-8') as train_file:
    dict_train = json.load(train_file)
    json.dump(data, train_file)

df_json = pd.DataFrame.from_dict(dict_train)

df_json.to_csv('Data_Collection\test_file1.csv')

如果名称 =='主要”:     app.run(host ='0.0.0.0',port = 8000,debug = True)

0 个答案:

没有答案