繁体   English   中英

Unity3D WWW错误C#

[英]Unity3D WWW Error C#

我在Unity中工作,试图弄清楚WWW类并从online-go.com访问API

我在Debug.Log中收到一个错误。 此外,第58行的Debug只会返回一个空白字符串。 我不完全了解如何使用WWW,因为这是我第一次使用它。

无法进行必要的数据回退UnityEngine.Debug:Log(Object) <LoadWWW>c__Iterator0:MoveNext() (位于Assets / 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;
         }
     }
 }

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM