2016-09-01 6 views
0

私のUnityゲームで私のWebサーバから値を読みたいのですが、私が望む応答が得られません。基本的には、私が示しているアプローチは基本的なデータ型ではうまく動作しますが、オブジェクトの配列(データベースから複数のint値を返す)はありません。WebサーバからのC#の応答が空白(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#のWebサービスでは、私はこれを行う:ユニティで

私はこの(完全なコード)を行う

[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サービスの一部のコードは、OK作品

そのブラウザで(それは私が欲しいものを返す)が、ユニティで、私は唯一の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自身の中で何かを見逃していますか?

EDIT:ブラウザ

enter image description here

+0

クライアント側のコードを共有してもよろしいですか? –

+0

私は自分のコードを編集しました。 – rootpanthera

+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 
    } 
+0

これはかなり簡単です。私はすでに私のコードでこれを持っています。 – rootpanthera

+0

私は、HttpResponseMessageを使用するサーバーコードのサンプルを使って自分の応答を更新しました。 –

関連する問題