来自网络服务器的C#响应为空(Unity)

时间:2016-09-01 13:53:20

标签: c# web-services unity3d

我想在Unity游戏中从我的网络服务器读取值,但我没有得到我想要的回复。基本上我将展示的方法适用于原始数据类型,但它不适用于对象数组(从我的数据库返回多个int值)。

在Unity中我这样做(完整代码):

void Start()
{
    string url = "http://example.com/unitygames/unitywebservice.asmx/GetGameData?='mygamename'";
    WWW www = new WWW(url);
    StartCoroutine(WaitForRequest2(www));
}


IEnumerator WaitForRequest2(WWW www)
{
    yield return www;

    if (www.error == null)
    {
        Debug.Log(www.text);
    }
    else
    {
          Debug.Log("Error " + www.text);
    }

在C#Webservice中,我这样做:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Int32> GetGameData(string gameName)
{

    List<Int32> myList = new List<Int32>();
    SqlConnection connection = new SqlConnection(SQL_CONNECTION);
    String selectData = "SELECT STATEMENT HERE ..";
    connection.Open();

    SqlCommand command = new SqlCommand(selectData, connection)
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read()) {

        myList.Add(reader.GetInt32(0));
        myList.Add(reader.GetInt32(1));
        myList.Add(reader.GetInt32(2));
        myList.Add(reader.GetInt32(3));
        myList.Add(reader.GetInt32(4));
        myList.Add(reader.GetInt32(5));
        myList.Add(reader.GetInt32(6));
        myList.Add(reader.GetInt32(7));

    }
    connection.Close();
    return myList;
}

Web服务部分的代码运行正常,因为我已经在浏览器中测试过它(它返回我想要的内容),但在Unity中我只能从www.text响应中获取:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com/" />
UnityEngine.Debug:Log(Object)
<WaitForRequest2>c__Iterator1:MoveNext() (at Assets/Networking.cs:69)

那么为什么我没有从www.text获得任何适当的回应?我错过了Unity本身的一些东西吗?

编辑:浏览器的实际结果

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试从您的服务HttpResponseMessage响应而不是普通列表返回:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public HttpResponseMessage GetGameData(string gameName)
{
    // Get data 
    List<Int32> myList = new List<Int32>();

    // ...

    // Convert myList to JSON or XML string
    // Example of simple JSON conversion for your case / better to use converter
    var json = string.Format("[{0}]", myList.Select(x=>x.ToString()).Aggregate((x,y) => x + ", " + y));

    // Create response
    var httpResponseMessage = new HttpResponseMessage();
    // Set content
    httpResponseMessage.Content = json;
    // Set headers content type
    httpResponseMessage.Content.Headers.ContentType =  = new MediaTypeHeaderValue("application/json"); // or "application/xml"
    // Set status
    httpResponseMessage.StatusCode = HttpStatusCode.OK;

    return httpResponseMessage;
}

然后你必须等到服务器下载响应完成后才能完成:

    public string url = "http://example.com/unitygames/unitywebservice.asmx/GetValues";

    IEnumerator Start() {
        // Start a download of the given URL
        WWW www = new WWW(url);

        // Wait for download to complete
        yield return www;

        // Read the www.text   
    }