[英]Unity 3D how to get/post a list from REST API
我在使用Unity 3D和C#從REST服務器獲取列表/向REST服務器發布列表時遇到問題。
我的服務器:
from flask import Flask, jsonify
import requests, json
app = Flask(__name__)
url = "http://0.0.0.0:5000/"
list = ["1","2","3","4"]
@app.route('/')
def index():
return "Hello"
@app.route('/list', methods=['GET'])
def get_tasks():
return jsonify(list)
if __name__ == '__main__':
app.run(host="0.0.0.0", port = 5000,debug=True)
如何在Unity中下載對象列表並將其保存到變量中? 另外,我該如何發布/更新另一個列表? https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html
在本教程中,我得到了“ Hello”文本返回鍵,但使用時它僅返回“ 1”
byte[] results = www.downloadHandler.data;
Debug.Log(results)
編輯:我的Python客戶端:
import json
import requests
list = " "
api_url = 'http://ipaddress:5000/list/'
r = requests.get(url = api_url, json=list)
a = r.content
if "3" in a:
print "found"
當客戶端運行時,它會打印“找到”,因此它返回錯誤列表。 在Unity中,我得到:[1,]
您說接收的數據是這種格式: ["1","2", etc.]
並且您希望在列表中用逗號分隔數據。
首先,這只是文本數據。 從www.downloadHandler.text
而不是www.downloadHandler.data
檢索文本數據。 刪除[
和]
然后用逗號將最終字符串分成數組或列表。
string results = www.downloadHandler.text;
string trimmed = results.TrimStart('[').TrimEnd(']');
List<string> splitResult = trimmed.Split(',').ToList<string>();
注意,您需要包括using System.Linq;
在代碼的頂部,以便使用上面的ToList
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.