0

I am working on an application for my web service. I work with LitJson, I finished the application but at the moment of exporting it to test it in my android does not work related to WWW IEnumerators.

For example, a button that starts the session, only the function that calls the IEnumerator works, but the IEnumerator itself does not work, and I have checked it by placing an alert message that it creates for the application.

This is the function that the button calls:

public void LogIn() {
    UserName = GameObject.Find("inputUsuario").transform.GetChild(2).gameObject.GetComponent < Text > ().text;
    Password = GameObject.Find("inputPassword").transform.GetChild(2).gameObject.GetComponent < Text > ().text;
    Debug.Log("Username:" + UserName + "And Password:" + Password);
    StartCoroutine(LogInWWW());
}

And this is the IEnumerator:

public IEnumerator LogInWWW() {
    WWW www = new WWW(UrlLogin + "?userName=" + UserName + "&password=" + Password);
    yield return www;
    print(www.text);
    if (www.error == null) {
        ProcessjsonLogin(www.text);
    } else {
        Debug.Log("ERROR: " + www.error);
    }
}

I must add that there is no error in the debug console on Android. Also I must clarify that to the URL I have added the prefix "http://" since it is a solution that give in the questions that I have seen.

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
Nodek
  • 21
  • 1
  • What is the value of the `UrlLogin` and `print(www.text)` on Android? If you get any error from `Debug.Log("ERROR: " + www.error);` you should post that as well. This will help answer your question. – Programmer Jun 26 '17 at 01:50
  • When you say it works on the editor, do you mean that it just compiles when you press play or it actualy does what you want – Hasan Emrah Süngü Jun 26 '17 at 01:51
  • the App not launching any value, The IEnumerator does not work completely (only on Android) In the editor it works correctly, it launches values ​​and I can complete the "Login" function correctly and the same happens with the other IEnumerators of the Application. – Nodek Jun 26 '17 at 01:56
  • The Value of UrlLogin is: http://cajashextech.esy.es/app/logIn.php and I do not get any error, it just does not work. – Nodek Jun 26 '17 at 01:58
  • Shouldn't this instruction be at end : yield return www;. You are returning before you ever execute ProcessjsonLogin(); – jdweng Jun 26 '17 at 01:58
  • @jdweng No. That seems fine. – Programmer Jun 26 '17 at 01:59
  • @Nodek Do not say "it doesn't work". Explain which part does not work. Don't say that the coroutine does not work. You question will remain unanswered if you continue that. print(www.text); or `Debug.Log("ERROR: " + www.error);` if that's null . See [this](https://stackoverflow.com/a/44690501/3785314) question for how to do that on Android. – Programmer Jun 26 '17 at 02:02
  • It would be correct if : yield return www => – jdweng Jun 26 '17 at 02:18

2 Answers2

1

Well guys, sometimes the errors are rare but in short, I managed to get to the error thanks to "trial and error", the problem was that I tried to get the value of UserName and Password using GetChild, like the following code:

  `Password = GameObject.Find("inputPassword").transform.GetChild(2).gameObject.GetComponent<Text>().text;`

For some reason on Android the object does not exist, I learned this because I was testing until part of the code I got the problem. Then I decided to change the code to find the text in a more specific way, changing the text object by inputUserTextObj, the same with the Password object and changing the code by:

    UserName = GameObject.Find("inputUsuarioTextObj").gameObject.GetComponent<Text>().text;
    Password = GameObject.Find("inputPasswordTextObj").gameObject.GetComponent<Text>().text;

As I said, I knew that the problem was not the IEnumerator, but it seems too strange that Debugging did not show me any error since it was a typical error of "GameObject is Null" and I could even swear to them by my life that Debug the application and I never showed the said error

But hey, thank you very much for your time guys, really feel the support and forgive me for taking your time.

Nodek
  • 21
  • 1
  • If you are running it on the device, don't expect the log to show on the Editor. I provided a link on the comment under question on how to find the logs on Android devices. Nice. Are you really sure this is really the problem? This is fixed now? – Programmer Jun 26 '17 at 04:01
  • Yes, enter the link but I swear I did not find anything, or maybe I did not search good:/ But, yes, that was the problem, and the whole application is running correctly without any problem, thanks :) – Nodek Jun 26 '17 at 04:09
  • Ok. You are welcome. It weird that `GetChild()` is failing on Android. Probably not getting the right GameObject. Will up-vote this answer since it solves the problem. Happy coding! – Programmer Jun 26 '17 at 04:09
-2

When you instantiate a new WWW(strung Url) it implicitly starts the request as well but it is not sync operation, it is async so you have to check www.isDone before you can use www.text. I think it works on Unity Editor properly because it just has a faster connection.

public IEnumerator Request(){
    using (var testRequest = new WWW(@"URL")) {
    var totalWaited = 0;
    var timeOuted = false;
    while (testRequest.isDone == false) {
        yield return new WaitForSeconds(1);
        totalWaited++;
        if (totalWaited > timeOutInSeconds) {
            timeOuted = true;
        }
    }
    if (timeOuted) { Debug.LogError("TimeOut"); yield break; }
    yield return testRequest.text;
}

This will prove some other users wrong. You can see that you will get an error if you access <code>.text</code> immediately

Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
  • Ok, I make a little text obj to Debug if `www.isDone`, and Again in the Editor works, but on Android no, it just does nothing. I debug the app and i cant get any error. it just does nothing. Now, I decide to replace all code of IEnumerator by a simple code: `public IEnumerator LogInWWW() { yield return new WaitForSeconds(1); GameObject.Find("DebugTextObj").gameObject.GetComponent().text = "www ins't Done yet"; }` And again it does not work on Android but in the Editor itself. – Nodek Jun 26 '17 at 02:55
  • I would like to know the reason for a minus so that I can update the code. The code perfectly works – Hasan Emrah Süngü Jun 26 '17 at 03:06
  • @Nodek, Can you check the updated code. The code works perfectly fine :) – Hasan Emrah Süngü Jun 26 '17 at 03:06
  • I just do not get why people down vote without giving proper explanations.. Just makes me want to delete / never give any answers. such toxic behaviours. – Hasan Emrah Süngü Jun 26 '17 at 03:09
  • Sorry, I did not put the negative vote. I do not doubt that it works, but the problem is not the IEnumerator, it just does not start. – Nodek Jun 26 '17 at 03:10
  • @Nodek I agree that people rush to down-vote but your answer is **not** correct. OP's original code is correct. Your answer made is look like OP's code is incorrect or he's doing it wrong. Again, OP's code with `yield return www` is correct. You use `www.isDone` when you [want](https://stackoverflow.com/questions/44682376/unity-waiting-for-http-request-to-resolve/44682772#44682772) to check the progress of the download. This ain't the problem. *"I think it works on Unity Editor properly because it just has a faster connection"* This is not true either. – Programmer Jun 26 '17 at 03:14
  • @Programmer, Can you tell me which part is `Incorrect`, `Not Working`.. I just gave an example about how it might be done. Moreover, OP's code is definitely going to give `UnityException: WWW is not ready downloading yet` error expecially on slow conenctions or big responses. Check your facts before you even answer. – Hasan Emrah Süngü Jun 26 '17 at 03:18
  • @Programmer, `WWW www = new WWW(URL); yield return www; print(www.text);` This line will give `UnityException: WWW is not ready downloading yet` error especially on slow connections or big responses. Try it yourself before you make baseless assumptions. – Hasan Emrah Süngü Jun 26 '17 at 03:19
  • I'm really sorry, I'm new and I wanted to solve your problem by adding a positive vote but it says that I'm new and that I can not vote until I have 15 reputation :/ As for the problem, your argument is good and I even change my code implementing the www.isDone however that is not the problem, I have many changes and I think I have already found the problem, I am working on that to be able to affirm what the problem is ... Even so, thank you very much for your time, I am very sorry for what happened, but neither I know who was the negative vote. – Nodek Jun 26 '17 at 03:21
  • @Nodek, It is OK, do not worry. I will pass this question to my team's senior Unity guys and let them a look. Cheers. – Hasan Emrah Süngü Jun 26 '17 at 03:23
  • *"you have to check www.isDone before you can use www.text"* and *"I think it works on Unity Editor properly because it just has a faster connection"* are incorrect. `isDone` is optional. I will **not** try it because OP's code is correct and this will only make the comment section long. See [this](https://docs.unity3d.com/ScriptReference/WWW.html) from the doc and you will see that his code is fine. The only person that can help fix this problem is @Nodek by posting the log I asked for under his comment otherwise, you are wasting your time to answer this question. – Programmer Jun 26 '17 at 03:24
  • @Programmer, I have seen people being wrong and being stubborn but you are on a different level :) You are even afraid to check the correct answer because you know it will Prove you wrong. You CAN NOT USE `.text` before `.isDone` is true. What I mean by `"you have to check www.isDone before you can use www.text"` is that if you try to access `.text` before `.isDOne` is true you will get exception. – Hasan Emrah Süngü Jun 26 '17 at 03:30
  • @Programmer, I have put up a screen shot which proves you wrong. So accept that you are wrong and try to positively contribute to this problem. – Hasan Emrah Süngü Jun 26 '17 at 03:36
  • My comment was to tell you are wrong and to explain why you likely got the down-vote. I did this so that you will edit your question and won't get more down-votes when Unity users return back on SO on Monday. I have been wrong many times and always admit it. **I suggest you look at the two links I provided before making further comments**. `yield return www` will wait until `WWW` request is done then you can use `www.text`. Finally, `isDone` is optional. – Programmer Jun 26 '17 at 03:38
  • Your screenshot is **useless** because you are doing `yield return www.text` instead of `yield return www`. Happy coding. Next time, make sure to check the comments and the links properly before making further comments. – Programmer Jun 26 '17 at 03:39
  • @Programmer, Your comments are useless because you fail to understand simple English. Happy coding to you! – Hasan Emrah Süngü Jun 26 '17 at 03:41
  • 2
    I got to backup @Programmer on this, the initial code has nothing wrong. Your code is wrong. You don't stall the coroutine with waitforseconds while loading WWW content, it does not makes any sense. The logic of timeOuted is irrelevant. At least if you would break the loop, I could see a starting of logic. The last line is useless, you don't yield content out of the coroutine. This is no IEnumerator like you use for a collection, if you needed to return data, you'd do it from callback or parameter assignment. Editor connection is most likely the same if he uses it in the same place. – Everts Jun 26 '17 at 09:38
  • 2
    @Everts You cannot correct this person as he will not listen. This kind of answer is why the down-vote button was made. – Programmer Jun 26 '17 at 09:46
  • @Everts _Your code is wrong_. It works. **I gave a working example.** I will repeat again if `.isDone` is false and you try to access `.text` you will get an exception. I never ever stated that `yield return www` will not be working. I never said this is the best solution – Hasan Emrah Süngü Jun 26 '17 at 10:22
  • 2
    var blueColor = Color.white; this is working but this is also wrong. – Everts Jun 26 '17 at 10:57
  • @Everts, _ var blueColor = Color.white; this is working but this is also wrong_. This sentence shows your quality as software developer. It is a perfectly fine local variable. I can even name it`var illitarateDeveloper = Color.white;`. It will work fine, compile fine. I suggest that you learn what _wrong_ means. Moreover, try to have education in a reputable university to learn basics about writing some code. – Hasan Emrah Süngü Jun 26 '17 at 11:58