- Home /
Question by
sandeepsmartest · May 12, 2014 at 06:57 PM ·
androidwwwxmltextasset
Guys please help me!!how to download xml file from url in android device runtime
Basically i want to read data from url and store it to xml file during runtime in android device.So that i can use the xml file in further reference in my android apk
And one more doubt -how to convert string to text asset
Below is a small trail that i did for xml download,but of no use
public class XmlTest : MonoBehaviour {
public XmlDocument DataFrmOnline;
string url;
public string[] str;
public string str1;
public TextAsset xmltextasset;
// Use this for initialization
void Start () {
url="myurl/apps.xml";
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.A))
StartCoroutine(IsFinishDownload(url));
}
IEnumerator IsFinishDownload (string url) {
WWW hs_get = new WWW(url);
yield return hs_get;
str1= hs_get.text;
try
{
DataFrmOnline = loadxmldocu(hs_get.text); //this works good if i input textasset instead of www.text
}
catch{}
}
public static XmlDocument loadxmldocu(TextAsset xmlfile)
{
MemoryStream assetstream = new MemoryStream (xmlfile.bytes);
XmlReader reader = XmlReader.Create(assetstream);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
return xmlDoc;
}
}
Comment
Best Answer
Answer by sandeepsmartest · Oct 21, 2015 at 07:08 AM
public Texture2D TextureContainer;
void Start()
{
StartCoroutine(FinishDownload_Image("ImageUrl",TextureContainer));
}
IEnumerator FinishDownload_Image (string url,Texture2D TexturToCopy, int no) {
WWW hs_get2 = new WWW(url);
yield return hs_get2;
//sun= hs_get.texture;
//hs_get.LoadImageIntoTexture(TexturToCopy);
//print("image url finsh__"+url);
TexturToCopy= hs_get2.texture;//TexturToCopy-consists of image that resides in the url
}
This is not about to work as you expect it.
TextureToCopy is of type Texture2D and when you pass the TextureContainer argument, the value is passed. So if TextureContainer is 0x00AA then TextureToCopy is 0x00AA.
Then you pass a new texture to it that is actually created by Unity somewhere else. So TextureToCopy is now 0x00BB but TextureContainer has not changed.
You need to use a delegate:
IEnumerator FinishDownload_Image (string url,Action<Texture2D> action, int no) {
WWW hs_get2 = new WWW(url);
yield return hs_get2;
action(hs_get2.texture);
}
and you call
void Start()
{
StartCoroutine(FinishDownload_Image("ImageUrl", result => TextureContainer = result));
}