【问题标题】:Unity3D WWW Error C#Unity3D WWW错误C#
【发布时间】:2016-05-13 03:58:20
【问题描述】:

我在 Unity 工作,试图找出 WWW 类并从 online-go.com 访问 API

我在 Debug.Log 中得到了一个错误。此外,第 58 行的调试只返回一个空白字符串。因为这是我第一次使用它,所以我认为我没有完全理解如何使用 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;
         }
     }
 }

【问题讨论】:

  • 请详细说明,在这里引用行号毫无意义,因为我们根本看不到 SO 中的行。还有什么错误?

标签: c# web unity3d


【解决方案1】:

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.

【讨论】:

    猜你喜欢
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多