WWW class can not access TXT file; please help.

Hey!
I have my problem like this:

The Txt file URL : __http://218.92.177.118:8000/houtai/txt/JS040000RS-GF-0001.txt__
The .unity3d file URL : __http://218.92.177.118:8000/TT/WebPlayer.unity3d__

WebPlayer.unity3d access the file by WWW class like this:

IEnumerator GetData()
{
WWW www= new WWW(“http://218.92.177.118:8000/houtai/txt/JS040000RS-GF-0001.txt”);
yield return www;
if (www.error == null || request.error == “”)
{

}
}

In UNITY EDITOR it works ,BUT when i published the project for WebPlayer, it does NOT work.
and i follow the
http://docs.unity3d.com/Manual/SecuritySandbox.html
create the xml file : __http://218.92.177.118:8000/crossdomain.xml__

<?xml version="1.0"?>

but it does not work too.

My Question:

1# : Is the c__rossdomain.xml__ file nesessary?

2#: crossdomain.xml is a ANSII or a UTF-8 file?

3#: what’s my problem, please help …

Please use Unity - Manual: Log files to find what error message is printed. Only thing I can think of without trying it myself (will do after work) is that it might be necessary to put it at :80 as I think unity is searching there for the file. If that is the case you could try Security.PrefetchSocketPolicy for different port.

The crossdomain.xml is only needed if you are using WWW to download files hosted on a different server from where the unity3d is hosted. The XML is documented as being ASCII. What errors do you get, and, as @billykater says, what does the log say?

thanks;
follow is the log file.

1851446–118730–log_a15f362b4436ac4fa74ecce3a186a88f.txt (9.38 KB)

log:

Platform assembly: C:\Users\guobochun\AppData\LocalLow\Unity\WebPlayer\player\Stable3.x.x\Data\lib\CrossDomainPolicyParser.dll (this message is harmless)
----above info is about the crossdomain, and no more about the detail of Policy Parser.

NullReferenceException: Object reference not set to an instance of an object
at iGUICode_1ceng+c__Iterator1.MoveNext () [0x00000] in :0
----because can not get file from server , some reference is NULL .

So there is nothing useful in that log. What happens in your code for you to say it doesn’t work? What’s request.error?

Platform assembly: C:\Users\guobochun\AppData\LocalLow\Unity\WebPlayer\player\Stable3.x.x\Data\lib\CrossDomainPolicyParser.dll (this message is harmless)
Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/crossdomain.xml

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Download had OK statuscode

Received the following crossdomain.xml


<?xml version="1.0"?>

received policy

Parsing: cross-domain-policy

cross-domain-policy

Parsing: allow-access-from

allow-access-from

domain: *

done parsing policy

crossdomain.xml was succesfully parsed

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Checking if http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt is a valid domain

Checking request-host: 218.92.177.118 against valid domain: *

All requirements met, the request is approved

Can you output www.error in the else clause of the if statement and add an additional log before doing the if, Debug.Log(request) should give more insight?

Judging from the null reference exception and the code given here my most likely guess is that request is null and not something inside the www class. Maybe some of the “referenced script on this behaviour is missing” errors cause this? Where is request set and what type is it?

follow is my code :

private IEnumerator LoadTextFromUrl()
    {
        WWW www = new WWW(this.urltext);
        yield return www; 
        if (www.isDone && www.error == null&&www.bytes!=null)
        {
            this.wenben =UTF8ToGB2312(www.bytes).Trim(new char[] {'?'}).Replace("常州","盐城");
            this.jsontext= JsonMapper.ToObject(this.wenben);

            label4.label.text = this.jsontext["ANTICIPATE_DAY"].ToString();//""
            //label9.label.text =  this.jsontext["TRANSACT_ADDR"];//
            textfield4.value =  this.jsontext["Imple_Suject"].ToString();//
            textfield5.value =  this.jsontext["Comm_Period"].ToString();//
            //label12.label.text =  this.jsontext["LINK_TEL"];//
            label7.label.text =  this.jsontext["TRANSACT_DEPNAME"].ToString();//
            label8.label.text =  this.jsontext["CHARGE_FLAG"].ToString();//
            label9.label.text =  this.jsontext["SUPERVISE_TEL"].ToString();//

            banlididian = this.jsontext["TRANSACT_ADDR"].ToString();
            lianxidianhua = this.jsontext["LINK_TEL"].ToString();


            fuwizhinan = this.jsontext["Service_Guide"].ToString();
            falvyiju = this.jsontext["BY_LAW"].ToString();   
            shoulitiaojian = this.jsontext["CONDITION"].ToString();   
            changjianwenti = this.jsontext["QUESTION_BY_ANSWER"].ToString();   
        }
        Debugger.Log (1, "www", www.error);

    }

when i use
Debug.Log (www.error);:wink:
replace
Debugger.Log(1, “www”, www.error);
Unity compile is failed like this :

Assets/iGUI/iGUICode_1ceng.cs(507,17): error CS0104: Debug' is an ambiguous reference between System.Diagnostics.Debug’ and UnityEngine.Debug' Assets/iGUI/iGUICode_1ceng.cs(507,17): error CS0103: The name Debug’ does not exist in the current context

To accurately track the error you could try to build in developer mode (should get you a stack trace with exact line number where it is crashing) or just do the old “one print after each code line” trick to find where it is crashing. From you code given here I don’t think this has anything to do with the www class at all but any of the fields are not set and that is why it is crashing. Might also be a good idea to output the raw text you receive.

Edit:
Hm, never used Debugger.Log and I don’t even know if it is working correctly in the webplayer. The normal unity way to output something in unity is using UnityEngine.Debug which of course conflicts with System.Diagnostics.Debug so you should search for how to resolve clashing namespace conflicts.

yield return www;
UnityEngine.Debug.Log(www.isDone);
UnityEngine.Debug.Log(www.error);
UnityEngine.Debug.Log(www.text);
if (String.IsNullOrEmpty(www.error))
{
    // do your stuff
}

logggg:

Platform assembly: C:\Users\guobochun\AppData\LocalLow\Unity\WebPlayer\player\Stable3.x.x\Data\lib\CrossDomainPolicyParser.dll (this message is harmless)
Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/crossdomain.xml

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Determining crossdomain.xml location for request: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Download had OK statuscode

Received the following crossdomain.xml


<?xml version="1.0"?>

received policy

Parsing: cross-domain-policy

cross-domain-policy

Parsing: allow-access-from

allow-access-from

domain: *

done parsing policy

crossdomain.xml was succesfully parsed

About to parse url: http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt

Checking if http://218.92.177.118:8000/houtai/txt/JS040000RS-SX-0030.txt is a valid domain

Checking request-host: 218.92.177.118 against valid domain: *

All requirements met, the request is approved

True

(Filename: C:/BuildAgent/work/d63dfc6385190b60/artifacts/WebPlayerGenerated/UnityEngineDebug.cpp Line: 49)

Null

(Filename: C:/BuildAgent/work/d63dfc6385190b60/artifacts/WebPlayerGenerated/UnityEngineDebug.cpp Line: 49)

{

“QL_Cata”:“社会保险-医疗保险”,

“QL_REG_ID”:“JS040000RS-SX-0030”,

“QL_NAME”:“基本医疗保险异地就医审批”,

}

(Filename: C:/BuildAgent/work/d63dfc6385190b60/artifacts/WebPlayerGenerated/UnityEngineDebug.cpp Line: 49)

NullReferenceException: Object reference not set to an instance of an object
at iGUICode_1ceng+c__Iterator1.MoveNext () [0x00000] in :0

(Filename: Line: -1)

is that mean my json parsing failed ?

private IEnumerator LoadTextFromUrl()
    {
        WWW www = new WWW(this.urltext);
        yield return www; 
        UnityEngine.Debug.Log(www.isDone);
        UnityEngine.Debug.Log(www.error);
        UnityEngine.Debug.Log(www.text);
        if(String.IsNullOrEmpty(www.error))
        {
            this.wenben =UTF8ToGB2312([SIZE=5][COLOR=#0080ff]www.bytes[/COLOR][/SIZE]).Trim(new char[] {'?'}).Replace("常州","盐城");
            this.jsontext= JsonMapper.ToObject(this.wenben);
           [SIZE=5][COLOR=#0080ff] UnityEngine.Debug.Log("after JsonMapper\n");[/COLOR][/SIZE]
            label4.label.text = this.jsontext["ANTICIPATE_DAY"].ToString();//""
            //label9.label.text =  this.jsontext["TRANSACT_ADDR"];//
            textfield4.value =  this.jsontext["Imple_Suject"].ToString();//
            textfield5.value =  this.jsontext["Comm_Period"].ToString();//
            //label12.label.text =  this.jsontext["LINK_TEL"];//
            label7.label.text =  this.jsontext["TRANSACT_DEPNAME"].ToString();//
            label8.label.text =  this.jsontext["CHARGE_FLAG"].ToString();//
            label9.label.text =  this.jsontext["SUPERVISE_TEL"].ToString();//

            banlididian = this.jsontext["TRANSACT_ADDR"].ToString();
            lianxidianhua = this.jsontext["LINK_TEL"].ToString();


            fuwizhinan = this.jsontext["Service_Guide"].ToString();
            falvyiju = this.jsontext["BY_LAW"].ToString();   
            shoulitiaojian = this.jsontext["CONDITION"].ToString();   
            changjianwenti = this.jsontext["QUESTION_BY_ANSWER"].ToString();   
            [SIZE=5][COLOR=#0080ff]UnityEngine.Debug.Log("before go out\n");[/COLOR][/SIZE]
        }
    }

because lack the log “after JsonMapper”

@black2stone
As you can see your json only has QL_Cata, QL_REG_ID and QL_NAME but you are accessing a lot of different fields e.g. ANTICIPATE_DAY or TRANSACT_DEPNAME, and this is most likely the error you are seeing (depending how your json library works for not existing fields.

@Graham-Dunnett Edit: Sorry my mistake ^^

 public static String Test(string s)
    {
    if (String.IsNullOrEmpty(s) == true)
        return "is null or empty";
    else
        return String.Format("(\"{0}\") is not null or empty", s);
    }

from : String.IsNullOrEmpty(String) Method (System) | Microsoft Learn

@@black2stone
As you can see your json only has QL_Cata, QL_REG_ID and QL_NAME but you are accessing a lot of different fields e.g. ANTICIPATE_DAY or TRANSACT_DEPNAME, and this is most likely the error you are seeing (depending how your json library works for not existing fields.

json data is too long ;so i cut it down…sooorry.

Find out where exactly it goes wrong.
Add debug logs to

  • Before UTF8ToGB2312 in line 10 of the posted code
  • After UTF8ToGB2312 in line 11
  • in the else block of the if

This should tell you exactly which line the problem is at.

@billykater @Graham-Dunnett Thank you for your Patience!

i have try lots of things as your say, AND finally,the problem is solved.

  • crossdomain policy is OK.
  • The LitJson’s runtime Behavior Confuse me,under debugging JsonMapper.ToObject worked very well,but in webplayer it throw some thing exception. then i try Using JsonReader

http://lbv.github.io/litjson/docs/quickstart.html
and failed too,i don’t know why . i give up the LitJson , using String class parsing www.text.

On web player the WWW class is implemented using the browser. On editor it is faked using curl. Typically if a browser knows a request is for a text asset it’ll tell unity to set bytes to null. You only really need to care about bytes if you are loading a binary asset. (Which is why my little code example ignored bytes since you are loading textual data.)