1

I am working on a multiplayer video game using Unity. I can successfully connect to a remote server using something like this :

public string GetToken(string user, string pass)
    {
        string uri = _serverUrl + "/Token/" + user + "/" + pass;
        UnityWebRequest www = UnityWebRequest.Get(uri);

        www.Send();
        WaitForSeconds w;
        while (!www.isDone)
            w = new WaitForSeconds(0.1f);
        string token = null;
        JObject o = JObject.Parse(www.downloadHandler.text);
        token = o.First.First.ToObject<string>();            

        return token;
}

This works fine when I am trying the code from the Unity Editor on my PC. I can successfully connect to the remote server which is in the WAN.

However when I try to deploy the game on an Android device. The connection fails. (I have been carreful to add the full internet access permission before building the Android apk).

Any idea what might be happening ?

Thanks !

Tengku Fathullah
  • 1,279
  • 1
  • 18
  • 43
ElRosbif
  • 353
  • 1
  • 6
  • 18
  • So what is the error log for connection fails ? It will be good if you update your question. Might be helpful for others to help as it is more clear. – Tengku Fathullah Sep 18 '17 at 10:49
  • When that is the problem, where can I see this log when it's running on Android ? – ElRosbif Sep 18 '17 at 11:00
  • What does your url look like? Can you post it here? – Programmer Sep 18 '17 at 12:05
  • the url looks like : "http://xxx.xxx.xxx.xxx:xxxx/MyServer"; Note that using android sdk monitor does not show anything in the logs – ElRosbif Sep 18 '17 at 12:21
  • Does the url have username and password in it? This is what I am trying to figure out – Programmer Sep 18 '17 at 12:31
  • The url is plain and with the code the full url looks like this : "http://xxx.xxx.xxx.xxx:xxxx/MyServer/Token/Username/Password". Why ? – ElRosbif Sep 18 '17 at 12:35
  • Because of [this](https://stackoverflow.com/questions/39482954/unitywebrequest-embedding-user-password-data-for-http-basic-authentication-not) but I guess that's not the problem now. What does your url start from? – Programmer Sep 18 '17 at 12:44
  • Oh I see, thanks for the suggestion...No, no embedded password indeed. My url starts with : "http://82.228.xxx.xxx:9001" – ElRosbif Sep 18 '17 at 12:48
  • Add `http://` before the url and see what happens. Also try again with `http://www` then your url and see what happens. Let me know for both of them. – Programmer Sep 18 '17 at 12:58
  • Sorry I meant I already had http:// before the url. I tried also http://www and the url without success (works from unity editor on my pc but not once deployed on the android device). Very weird... could it be permission related ? – ElRosbif Sep 18 '17 at 13:09
  • I suspect that this because no "http" prefix in the url and is therefore a duplicate. Check the duplicate. If you think otherwise then use a simple `Text` component to print the `UnityWebRequest.error` value. Modify your question and tell us what it is. Also, you can use `Debug.Log` to print it then use [this](https://stackoverflow.com/questions/44690357/how-to-read-debug-log-when-using-android/44690501?s=1|4.2601#44690501) method and retrieve the value of the error. – Programmer Sep 18 '17 at 13:13
  • Found the problem. Looks like Android can't load the version of JSON I am using. Now I need to figure out how to fix it ! 09-18 15:22:00.959: I/Unity(19118): TypeLoadException: Could not load type 'Newtonsoft.Json.Linq.JObject' from assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. 09-18 15:22:00.959: I/Unity(19118): at GameManager.Awake () [0x00000] in :0 09-18 15:22:00.959: I/Unity(19118): 09-18 15:22:00.959: I/Unity(19118): (Filename: Line: -1) – ElRosbif Sep 18 '17 at 13:24
  • Do the-same thing with `UnityWebRequest.text` to see if you are getting the correct value before using the json(ToObject) function on the result. If you are getting the correct result as the Editor then the problem is just Json API. Use Unity's [JsonUtility](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity/36244111#36244111) for this. I have to go now and I think you can do the rest from here. If not, I will reply to your comment when I come. Question re-opened! – Programmer Sep 18 '17 at 13:28

1 Answers1

0

Actually it was a red herring. The problem was not related to the web connection but to json.net I picked the .net 2.0 version of the library, went into unity Edit->Projet Settings-> Player -> Other Settings and changed the API Compatibility level from 2.0 Subset to 2.0.

That did the trick ! Hope it helps someone in the future.

ElRosbif
  • 353
  • 1
  • 6
  • 18