- Home /
Question by
yuma_yanagisawa · Mar 13, 2016 at 08:54 PM ·
androidwwwsqlitewrite data
Write file with WWW on Android
The code below works well on PC, however, not on Android. The app already has a file at the "androidPath", and even after www, file never changed. What am I missing here?
using UnityEngine;
using System.Collections;
using System.IO;
public class Downloader : MonoBehaviour {
string url = "http://SOMEDOMAIN.com/_inc/_sqlite/nexco.db3";
// Use this for initialization
void Start () {
// test method to dawnload png file.
sendGetRequest();
}
// Update is called once per frame
void Update () {
}
private IEnumerator TestDownload(WWW www){
//yield return www;
// already method has got the WWW
while (!www.isDone) {
Debug.Log("downloaded " + (www.progress*100.0).ToString() + "%...");
yield return null;
}
// set path to download item.
if (www.isDone) {
#if UNITY_EDITOR
string editorPath = Application.dataPath + "/StreamingAssets/nexco.db3";
// first, delete file..
File.Delete(editorPath);
File.WriteAllBytes(editorPath, www.bytes);
#elif UNITY_ANDROID
string DatabaseName = "nexco.db3";
var filepath = string.Format("{0}/{1}", Application.persistentDataPath, DatabaseName);
string androidPath = "jar:file://" + Application.dataPath + "!/assets/" + DatabaseName;
// first delete file
File.Delete(androidPath);
File.WriteAllBytes(androidPath, www.bytes);
#endif
}
Debug.Log("download done!");
}
/*
* www thing
* methods to handle with data
*/
void sendGetRequest(){
WWWForm form = new WWWForm ();
form.AddField ("Username", "SOMEUSER");
form.AddField ("Password", "SOMEPASS");
var headers = form.headers;
//var rawData = form.data;
string secretKey = "SOMEUSER" + ":" + "SOMEPASS";
headers["Authorization"]="Basic " + System.Convert.ToBase64String(
System.Text.Encoding.ASCII.GetBytes(secretKey));
// in case request is GET not POST, the second parameter could be null.
WWW www = new WWW(url, null, headers);
//yield
StartCoroutine(TestDownload(www));
}
}
Comment
Your answer
Follow this Question
Related Questions
Best Way To Store Credentials and Data Online? 1 Answer
How to create a SQLite database in Android 0 Answers
www return empty text on android 0 Answers
SQLite works fine in Unity Editor but doesn't work in Android device (apk). 0 Answers
How can i do youtube video player from video url for Android devices ? 0 Answers