提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
unity中C#用WWW向django后端提交数据并存入mysql数据库
前言
主要解决发送多条数据,如果数据为空值暂时不接收,主要为判断了接收到数据的字符串长度
提示:以下是本篇文章正文内容,下面案例可供参考
一、Unity中C#(WWW)方法发送数据请求的数据请求
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SubmitKPbaoGao : MonoBehaviour
{
public GameObject FinalScore;
// Start is called before the first frame update
public void OnClick()
{
StartCoroutine(TimeToSendMessage2());
}
IEnumerator TimeToSendMessage2()
{
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content_Type", "application/x-www-form-urlencoded");
string data2 = "yx_score=" + "" + "&" + "cc_cz_time=" + "" + "&" + "cc_cz_score=" + FinalScore.GetComponent<Text>().text + "&" + "cc_gg_score=" + "" + "&" + "zz_cz_time=" + "" + "&"
+ "zz_cz_score=" + "" + "&" + "zz_gg_score=" + "" + "&" + "kp_cz_time=" + "" + "&" + "kp_cz_score=" + "" + "&" + "kp_gg_score=" + "" + "&";
;
byte[] bs2 = System.Text.UTF8Encoding.UTF8.GetBytes(data2);
WWW www2 = new WWW("http://127.0.0.1/cccd", bs2, headers);
yield return www2;
{
if (www2.error != null)
{
yield return null;
Debug.Log("WWW2null");
}
else
{
Debug.Log("WWW2ok");
}
}
}
}
发送多条数据,采用拼接字符串的形式,存在data里面
二、django后端接收view
class Cccd(View):
//配置自己的视图处理函数
def get(self ,request):
return render(request,'需要返回会的页面.html')
def post(self,request):
score1 = str(request.POST['yx_score'])
score2 = str(request.POST['cc_cz_time'])
........................................
//上面C#代码拼接了几条数据就可以就收几条
username = requset.COOKIES.get('username')
user = User.objects.get(username = username)
//从登录的cookies中获取用户名信息,把数据存入对应的用户
if len(score1)>0:
//判断字符串长度大于0,满足的话进行储存(及判断是否为空)
user.score1 = score1
user.save()
if len(score2)>0:
user.score2 = score1
user.save()
return HttpResponse('ok')
# 总结
<font color=#999AAA >提示:这里对文章进行总结:
主要为if len(score1)>0:判断接收到的值不为空,进行存储