2016-05-13 56 views
0

我团结奋斗试图弄清楚从online-go.comUnity3D WWW错误C#

的WWW类和访问API我在得到的debug.log一个错误,但。此外,58行调试只是返回一个空白字符串。我不认为我完全理解如何使用WWW,因为这是我第一次使用它。

必要的数据倒带是不可能 UnityEngine.Debug:Log(Object) <LoadWWW>c__Iterator0:MoveNext()(在资产/ OGS.cs:60)

using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System; 
using System.IO; 
using System.Net; 
using System.Text; 
//using System.Net.httpclient; 

public class OGS : MonoBehaviour { 

    string generateAPIClient = "http://beta.online-go.com/developer"; 
    string APIKey = "0c63a59dd17ec69a48af5d9dc8b4e956"; 
    string requestUserToken = "oauth2/access_token"; 
    string clientID = ""; 
    string clientSecret = ""; 
    string baseURL = "http://online-go.com/"; 
    string url = ""; 
    string username; 
    string password; 
    string POST; 

    List<Settings> settings; 
    // Use this for initialization 
    void Start() { 
     Debug.Log("Opened"); 
     settings = new List<Settings>(); 
     Load("Settings"); 
     clientID = AssignSetting("clientID"); 
     clientSecret = AssignSetting("clientSecret"); 
     username = AssignSetting("username"); 
     password = AssignSetting("password"); 
     POST = string.Format( "client_id={0}&client_secret={1}&grant_type=password&username={2}&password={3}", 
           clientID, clientSecret, username, password); 
     url = baseURL + requestUserToken; 
     StartCoroutine("LoadWWW"); 

    } 

    //Assign settings loaded to settings variables 
    string AssignSetting (string item) { 
     int position = -1; 
     for(int i=0;i<settings.Count;i++) { 
      if(settings[i].name == item){return settings[i].value;} 
     } 

     return string.Empty; 
    } 

    IEnumerator LoadWWW() { 
     byte[] byteArray = GetBytes(POST); 
     Dictionary<string,string> headers = new Dictionary<string,string>(); 
     headers.Add("Content-Type", "application/x-www-form-urlencoded"); 
     WWW text = new WWW(url, byteArray, headers); 
     yield return text; 
     byteArray = text.bytes; 
     string POSTResponse = GetString(byteArray); 
     Debug.Log(POSTResponse); 
     Debug.Log(text.responseHeaders); 
     Debug.Log(text.error); 
    } 

    static byte[] GetBytes(string str) 
    { 
     byte[] bytes = new byte[str.Length * sizeof(char)]; 
     System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); 
     return bytes; 
    } 

    static string GetString(byte[] bytes) 
    { 
     char[] chars = new char[bytes.Length/sizeof(char)]; 
     System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); 
     return new string(chars); 
    } 

    private bool Load(string fileName) 
    { 
    // Handle any problems that might arise when reading the text 
    try 
    { 
     string line; 
     // Create a new StreamReader, tell it which file to read and what encoding the file 
     // was saved as 
      StreamReader theReader = new StreamReader(Application.dataPath + "/Resources/" + fileName + ".txt"); 
     // Immediately clean up the reader after this block of code is done. 
     // You generally use the "using" statement for potentially memory-intensive objects 
     // instead of relying on garbage collection. 
     // (Do not confuse this with the using directive for namespace at the 
     // beginning of a class!) 
     using (theReader) 
     { 
      // While there's lines left in the text file, do this: 
      do 
      { 
       line = theReader.ReadLine(); 

       if (line != null) 
       { 
        // Do whatever you need to do with the text line, it's a string now 
        // In this example, I split it into arguments based on comma 
        // deliniators, then send that array to DoStuff() 
        string[] entries = line.Split(':'); 
        if (entries.Length > 0){ 
          Settings newSetting = new Settings(entries[0], entries[1]); 
          settings.Add(newSetting); 
         } 
       } 
      } 
      while (line != null); 
      // Done reading, close the reader and return true to broadcast success  
      theReader.Close(); 
      return true; 
      } 
     } 
     // If anything broke in the try block, we throw an exception with information 
     // on what didn't work 
     catch (Exception e) 
     { 
      Console.WriteLine("{0}\n", e.Message); 
      return false; 
     } 
    } 
} 
+0

请详细说明,参考行号在这里没有意义,因为我们根本看不到SO中的行。你还有什么错误? –

回答

0

necessary data rewind wasn't possible当WWW呼叫期间参与重定向主要发生。

要解决此问题,请确保您调用的URL不会将您重定向到过程中的另一个页面。另外,在使用该值之前进行一些错误处理是一个好主意。

// wait for the result 
yield return text; 

// Handle the error if there is any 
if (!string.IsNullOrEmpty(text.error)) { 
    Debug.Log(text.error); 
} 
// Now do with POSTResponse whatever you want if there were no errors.