[英]Unity 3D how to get/post a list from REST API
Im having a problem with get/post a list from/to REST server with Unity 3D and C#. 我在使用Unity 3D和C#从REST服务器获取列表/向REST服务器发布列表时遇到问题。
My server: 我的服务器:
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)
How do i Download the list of objects in Unity and save it to a variable? 如何在Unity中下载对象列表并将其保存到变量中? Also how do i post/update another list? 另外,我该如何发布/更新另一个列表? https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html
with this tutorial i got the "Hello" text returnet but it only returns "1" when using 在本教程中,我得到了“ Hello”文本返回键,但使用时它仅返回“ 1”
byte[] results = www.downloadHandler.data;
Debug.Log(results)
edit: my Python client: 编辑:我的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"
When client is ran, it prints "found" so it returns the wole list. 当客户端运行时,它会打印“找到”,因此它返回错误列表。 in Unity i get: [1,] 在Unity中,我得到:[1,]
You said that the received data is this format: ["1","2", etc.]
and you want the data separated by comma in a List. 您说接收的数据是这种格式: ["1","2", etc.]
并且您希望在列表中用逗号分隔数据。
First, this is just a text data. 首先,这只是文本数据。 Retrieve the text data from www.downloadHandler.text
instead of www.downloadHandler.data
. 从www.downloadHandler.text
而不是www.downloadHandler.data
检索文本数据。 Remove the [
and ]
then split the final string by comma into array or list. 删除[
和]
然后用逗号将最终字符串分成数组或列表。
string results = www.downloadHandler.text;
string trimmed = results.TrimStart('[').TrimEnd(']');
List<string> splitResult = trimmed.Split(',').ToList<string>();
Note that you need to include using System.Linq;
注意,您需要包括using System.Linq;
at the top of the code in order to use the ToList
function above. 在代码的顶部,以便使用上面的ToList
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.