簡體   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