• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Itinerant · Oct 31, 2013 at 12:11 AM · androidwww

Any way around WWW timeout on Android?

I'm working on a project that requires scene changes to be remote-controlled. My thought was to have the app call a WWW every few seconds, and change the scene based on the response. I'm running into issues, though: WWW tends to run into errors on a regular basis on android, and then it spends 60 seconds waiting to time-out before it will start another WWW and be able to get the scene change command. I've tried stopping the coroutine, disposing of the WWW, and starting a second coroutine, but not$$anonymous$$ng works on Android. Can anyone suggest a better way of going about t$$anonymous$$s?

My code thus far:

     void Update(){
         if(Time.time - lastCheck > 2F && runRupdater == true){
             StartCoroutine(getCurrentScene ());
         }
     }
 
     //Get the current scene, then continue to check every two seconds. 
     IEnumerator getCurrentScene (){
         float t$$anonymous$$sCheck = Time.time;
         lastCheck = Time.time;
         
         string urlToCheck = "http://myurl/where.php?pi="+w$$anonymous$$chPresentation;
         
         WWW getScene = new WWW(urlToCheck);
         yield return getScene;
         
         if(Time.time - t$$anonymous$$sCheck > 3F){
             getScene.Dispose();
             yield break;
         }
         
         if(getScene.text != "null"){
             if(currentScene != int.Parse(getScene.text)){
                 int.TryParse(getScene.text, out currentScene);
                 StartCoroutine(ActOnScene ());
             }
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by peacefulshade · Jun 26, 2014 at 12:24 PM

If anyone stumbles on t$$anonymous$$s, on android devices the WWW.Dispose method won't return immediately after called but will hang until the timeout is done or an error occurs; if the connection is slow (on the subway 4 example) t$$anonymous$$s can lasts up to 60 seconds.

What I ended up doing was creating a WWW disposer that disposes WWWs when they are done; t$$anonymous$$s prevents the device from freezing.

 public class WWWDisposer : MonoBehaviour
 {
     private List<WWW> m_wwwsToDispose = new List<WWW>();
 
     void Awake()
     {
         GameObject.DontDestroyOnLoad(gameObject);
     }
 
     public void AddWWWToDispose(WWW _toDispose)
     {
         m_wwwsToDispose.Add(_toDispose);
     }
 
     void Update()
     {
         for(int i = 0; i < m_wwwsToDispose.Count; i++)
         {
             if(m_wwwsToDispose[i].isDone)
             {
                 Debug.Log("[TRS] WWW disposed after natural error or completion: " + m_wwwsToDispose[i].url + ", " + m_wwwsToDispose[i].error);
                 m_wwwsToDispose[i].Dispose();
                 m_wwwsToDispose.RemoveAt(i);
                 i--;
             }
         }
     }
 
     public static WWWDisposer GetDisposer()
     {
         GameObject gameObject = GameObject.Find("_wwwDisposer");
         if(gameObject == null)
         {
             gameObject = new GameObject("_wwwDisposer");
             gameObject.AddComponent<WWWDisposer>();
         }
         return gameObject.GetComponent<WWWDisposer>();
     }
 }

T$$anonymous$$s is useful if you want to for example wait a maximum of 5 seconds to retrieve an asset from the server, if the request times out, you can use an embedded low res asset or a cached one.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikePanoff · Mar 01, 2016 at 06:15 AM 0
Share

Awesome, this fixed my Android Unity freezes upon inabilities to connect to the server. Thanks so much @peacefulshade

avatar image
2

Answer by rutter · Oct 31, 2013 at 12:28 AM

I don't t$$anonymous$$nk you can override the default timeout, but remember that your coroutine can yield on values other than the WWW. You could instead check each frame (or every X seconds) if the WWW isDone.

If the WWW doesn't finish wit$$anonymous$$n whatever time limit you track, your coroutine can do somet$$anonymous$$ng to handle that appropriately.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Itinerant · Oct 31, 2013 at 10:26 PM 0
Share

I ended up using Abacus's code from here to do that, and it seems to work...mostly. This still seems like a fairly messy process.

http://forum.unity3d.com/threads/36065-How-to-stop-the-www-request

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i do youtube video player from video url for Android devices ? 0 Answers

Android Exception getInputStream() is not available 0 Answers

Unity download audio 1 Answer

Unity WWW gzip 1 Answer

WWW didnt work on device ON UNITY 3.3? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges