WWW尚未准备好下载

时间:2015-03-23 19:07:03

标签: c# unity3d

我试图从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我执行协程后使用它的字段??我想在标签中使用这个filds。

1 个答案:

答案 0 :(得分:0)

您的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回调,但你可以轻松添加它。