如何在Flutter中发布Json数据数组

时间:2020-07-27 13:21:31

标签: flutter

这是我的数据

all_google_accesses = AccessRecord.objects.filter(webpage__url='google.com')

我需要将这些数据通过Flutter移动应用发布到网络服务器上

这是我尝试的代码

var sending files = 
[
{"filename": "manifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\extension\\manifest.json", "fileCreationDate": "Fri Jul 24 10:44:50 2020", "fileLastModifiedDate": "Sat Jul 25 09:35:23 2020"}, 
{"filename": "manifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\fcm-extension-master\\fcm-extension-master\\manifest.json", "fileCreationDate": "Sun Sep  2 22:52:18 2018", "fileLastModifiedDate": "Thu Jul 23 16:09:07 2020"},
{"filename": "manifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\fcm-javascript-master\\manifest.json", "fileCreationDate": "Thu Jul 23 21:27:24 2020", "fileLastModifiedDate": "Thu Jul 23 21:29:09 2020"},
{"filename": "web-app-manifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\files required for vue working\\node_modules\\caniuse-db\\features-json\\web-app-manifest.json", "fileCreationDate": "Sat Jan 25 15:38:44 2020", "fileLastModifiedDate": "Sat Oct 26 13:45:00 1985"},
{"filename": "AssetManifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\flutter apps\\myapp\\build\\app\\intermediates\\flutter\\debug\\flutter_assets\\AssetManifest.json", "fileCreationDate": "Tue Jun 23 21:58:29 2020", "fileLastModifiedDate": "Tue Jun 23 21:58:29 2020"}, 
{"filename": "FontManifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\flutter apps\\myapp\\build\\app\\intermediates\\flutter\\debug\\flutter_assets\\FontManifest.json", "fileCreationDate": "Tue Jun 23 21:58:29 2020", "fileLastModifiedDate": "Tue Jun 23 21:58:29 2020"}, 
{"filename": "AssetManifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\flutter apps\\myapp\\build\\app\\intermediates\\merged_assets\\debug\\out\\flutter_assets\\AssetManifest.json", "fileCreationDate": "Tue Jun 23 21:58:35 2020", "fileLastModifiedDate": "Tue Jun 23 21:58:35 2020"}, 
{"filename": "FontManifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\flutter apps\\myapp\\build\\app\\intermediates\\merged_assets\\debug\\out\\flutter_assets\\FontManifest.json", "fileCreationDate": "Tue Jun 23 21:58:35 2020", "fileLastModifiedDate": "Tue Jun 23 21:58:35 2020"},
{"filename": "manifest.json", "filepath": "C:\\Users\\lenovo\\Desktop\\my-extension\\manifest.json", "fileCreationDate": "Fri Jul 24 21:54:59 2020", "fileLastModifiedDate": "Fri Jul 24 21:54:32 2020"}
]

我也尝试过var response = await http.post(url,body:sendingFiles); 如果我使用

convert.jsonEncode(sendingFiles)

我得到以下输出。

var response = await http.post(url,body:convert.jsonEncode(sendingFiles));

1 个答案:

答案 0 :(得分:0)

请检查以下代码。 您可以使用此代码将JSON主体从flutter请求传递到服务器。另外,您可以向您传递身份验证令牌(如果使用的是任何令牌)。

Future<dynamic> getMillionDollarData(String token, String jsonBody) async {
 HttpClientRequest request = await client
    .postUrl(Uri.parse('server-url.com/someendpoint'));

 request.headers.set('Content-Type', 'application/json');
 request.headers.set('my-extra-token', token);

 HttpClientResponse response = await request.close();

 request.add(utf8.encode(json.encode(jsonBody)));

 String reply = await response.transform(utf8.decoder).join();
 return json.decode(reply);
}

要在python服务器端接收JSON

from flask import Flask, request

@app.route('/someendpoint'])
def someendpoint():
  data = request.json