2016-05-13 12 views
0

Ich arbeite in Unity versucht, die WWW-Klasse, um herauszufinden, und Zugang API von online-go.comUnity3D WWW Fehler C#

ich aber einen Fehler in der debug.log bekommen. Darüber hinaus gibt das Debug on Line 58 nur eine leere Zeichenfolge zurück. Ich glaube nicht, dass ich vollständig verstehe, wie man WWW benutzt, da dies das erste Mal ist, dass ich es benutze.

Notwendige Daten zurückspulen war nicht möglich UnityEngine.Debug:Log(Object) <LoadWWW>c__Iterator0:MoveNext() (bei Aktiva/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

Bitte sorgfältig, Referenz Zeilennummern ist hier sinnlos, wie wir die Linien in SO überhaupt nicht sehen können. Welchen Fehler bekommst du auch? –

Antwort

0

necessary data rewind wasn't possible hauptsächlich tritt auf, wenn die Umleitung während des WWW Anruf beteiligt ist.

Um dies zu beheben, stellen Sie sicher, dass die von Ihnen aufgerufene URL Sie nicht auf eine andere Seite im Prozess umleitet. Es wäre auch eine gute Idee, eine Fehlerbehandlung durchzuführen, bevor Sie den Wert verwenden.

// 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.