代码之家  ›  专栏  ›  技术社区  ›  AFS

WWW尚未准备好下载

  •  0
  • AFS  · 技术社区  · 11 年前

    我正在尝试从web服务获取一些json,但当我尝试使用result时,我收到了这个错误。text它会出现错误“WWW尚未准备好下载”如果我不尝试使用这个变量,我不会收到任何错误

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using Boomlagoon.JSON;
    
    public class ClassInfoScript : MonoBehaviour {
    
    private IEnumerator coroutine;
    public string name;
    private bool showInfo = false;
    //esta variable es para controlar que solo seconecte uan vez al entrar en un collider
    private bool connected = false;
    private bool isClass = false;
    private bool isDependency = false;
    private bool isOffice= false;
    public GUIStyle myStyle;
    public GUIStyle labelStyle;
    enum Types{classroom, dependency, office};
    
    
    void Start(){
    
    }
    void OnGUI() {
        if (showInfo) {
            GUI.Box (new Rect (Screen.width * 0.02f, Screen.height * 0.02f, Screen.width * 0.15f, Screen.height * 0.75f), "", myStyle);
            WWW result = null;
            if (isClass) {
                if(!connected){
                    result = get ("http://localhost/ws/get/clase/D1");
    
                }
                //I get the error here when I try to use result.text
                JSONObject jo = JSONObject.Parse(result.text);
            connected = true;
        }
    }
    //obtener el nombre
    public void setName(string value){
        name = value;
    }
    //cambiar el nombre
    public string getName(){
        return name;
    }
    //cambiar el  valor de showInfo
    public void changeInfoState(){
        showInfo = !showInfo;
    }
    public void changeClass(){
        isClass = !isClass;
    }
    public void changeDependency(){
        isDependency = !isDependency;
    }
    public void changeOffice(){
        showInfo = !showInfo;
    }
    public WWW get(string url){
        WWW w = new WWW (url);
        StartCoroutine(AskWebservice(w));
        return w;
    }
    
    IEnumerator AskWebservice(WWW w){
        Debug.Log ("Coroutine started");
        yield return w;
        if (w.error == null){
            Debug.Log("WWW Ok!: " + w.text);
        } else {
            Debug.Log("WWW Error: "+ w.error);
        }    
    
    }
    
    }
    

    现在我的onGui函数是这样的

    void OnGUI() {
        if (showInfo) {
            GUI.Box (new Rect (Screen.width * 0.02f, Screen.height * 0.02f, Screen.width * 0.15f, Screen.height * 0.75f), "", myStyle);
            WWW result = null;
            JSONObject jo = null;
    
            if (isClass) {
                if(!connected){
                    string url = "http://localhost/ws/get/clase/"+getName();
                    StartCoroutine(HandleWWWRequest(url, (www) => {
                        jo = JSONObject.Parse(www.text);
                        Debug.Log(jo.GetString("info"));
                        connected = true;
                    }));
                }
                GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.04f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("nombre"), labelStyle);
                GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.15f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("info"), labelStyle);
    
            } else if (isDependency) {
                /*coroutine = AskWebservice ("dependencia", name, Types.dependency);
                if(!connected)
                    StartCoroutine (coroutine);
    
                GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.04f, Screen.width * 0.15f, Screen.height * 0.75f), response [0], labelStyle);
                GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.15f, Screen.width * 0.15f, Screen.height * 0.75f), response [1], labelStyle);
                GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.20f, Screen.width * 0.15f, Screen.height * 0.75f), response [2], labelStyle);*/
            }
            connected = true;
        }
    }
    

    但在这两行中

    GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.04f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("nombre"), labelStyle);
    GUI.Label (new Rect (Screen.width * 0.03f, Screen.height * 0.15f, Screen.width * 0.15f, Screen.height * 0.75f), jo.GetString("info"), labelStyle);
    

    我不能在标签中使用jsonobject字段,它是空的,我已经尝试将这个字段存储在另一个变量和列表中,但它不起作用,我如何在执行协程后存储jsonobject字段以使用它??我想在标签上使用这个滤镜。

    1 回复  |  直到 11 年前
        1
  •  0
  •   Huacanacha    11 年前

    你的 get 函数是非阻塞调用。例如,StartCoroutine需要与yield一起使用 yield return StartCoroutine(...) ,等待代码等待协同程序完成。因为WWW还没有完成,所以还没有结果。

    尝试以下操作:

    IEnumerator HandleWWWRequest(string url, System.Action<WWW> onSuccess) {
        WWW www = new WWW(url);
        yield return www;
        if (string.IsNullOrEmpty(www.error)) {
            onSuccess(www);
        } else {
            Debug.LogWarning("WWW request returned an error.");
        }
    }
    
    ...
    
    if (isClass) {
        if(!connected){
            string url = "http://localhost/ws/get/clase/D1";
            // Non-blocking... success handling code is passed as a lambda
            StartCoroutine(HandleWWWRequest(url, (www) => {
                JSONObject jo = JSONObject.Parse(www.text);
                connected = true;
            }));
        }
    }
    

    为了保持代码清晰,我没有包含onError回调,但您可以轻松添加它。