Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UnityWebRequest Android: java.io.IOException: unexpected end of stream on Connection

Discussion in 'Android' started by Osetinski, Jun 6, 2018.

  1. Osetinski

    Osetinski

    Joined:
    Dec 19, 2017
    Posts:
    4
    Hello guys,

    I have a project where I communicate with some server over UnityWebRequest. In the editor everything works fine, but when I build it for Android and run it on my device I get an IOException sometimes while trying to send a web request. The exact error is as follows:
    I create the request through:
    Code (CSharp):
    1. UnityWebRequest.Post(url, data)
    where data is my dictionary with data (and url is the url of course). For sending I use
    Code (CSharp):
    1. public static IEnumerator SendRequestAsync(UnityWebRequest webRequest) {
    2.     // Don't chunk the data
    3.     webRequest.chunkedTransfer = false;
    4.  
    5.     // Add API key to the header
    6.     webRequest.SetRequestHeader(API_KEY_HEADER_NAME, API_KEY);
    7.  
    8.     // Send the request
    9.     yield return webRequest.SendWebRequest();
    10.     // From here on webRequest.isNetworkError is true when the error happend
    11.  
    12.     // Some more processing of the request
    13. }
    If the error appears, then after my yield (line 9) the property isNetworkError of UnityWebRequest is set to true. I found some references to this kind of IOException, where people said they solved it by setting keep-alive to true (in native Android mostly), but it seems like UnityWebRequest doesn't support this. If I create a new request to the same url and with the same data after the error, the request runs fine. Still I get the error often and I would love to know why it happens and how I maybe can prevent it.
    Thank you guys in advance!

    Regards,
    Michael
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,706
    Is that all information that you can see in logcat?
    Also, it would help if I could see more of the code that does the request, preferably with the data and all headers that you set.
     
  3. Osetinski

    Osetinski

    Joined:
    Dec 19, 2017
    Posts:
    4
    Yes this is all information to this error in my logcat. The function that constructs the request and calls the function mentioned above is as follows:

    Code (CSharp):
    1. public void CreateGameRequest(string endPoint, GameData gameData, string userToken) {
    2.     m_dataDictionary.Clear();
    3.     m_dataDictionary["gamegridID"] = gameData.GridID.ToString();
    4.     m_dataDictionary["x"] = gameData.row.ToString();
    5.     m_dataDictionary["y"] = gameData.column.ToString();
    6.     m_dataDictionary["history"] = gameData.history;
    7.     UnityWebRequest webRequest = UnityWebRequest.Post(URL + endPoint, m_dataDictionary);
    8.     webRequest.SetRequestHeader("token", userToken);
    9.     StartCoroutine(SendRequestAsync(webRequest));
    10. }
    GameData is just some simple class wrapping some primitive values:

    Code (CSharp):
    1. public class GameData {
    2.     public int GridID = -1;
    3.     public int row = -1;
    4.     public int column = -1;
    5.     public string history = "";
    6. }
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,706
    And the issue only happens when you run on Android? Never in editor?
     
  5. Osetinski

    Osetinski

    Joined:
    Dec 19, 2017
    Posts:
    4
    No I never had it in editor, but on at least two different Android devices. It doesn't seem to happen on iOS either.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,706
    Sounds worth of a bug report.
     
  7. bardia68

    bardia68

    Joined:
    Apr 1, 2016
    Posts:
    7
    I get the same error too:

    java.io.IOException: unexpected end of stream on Connection{91.121.33.92:80, proxy=DIRECT@ hostAddress=91.121.33.92 cipherSuite=none protocol=http/1.1} (recycle count=0)

    It only shows on android device (never on editor). I have a simple PUT method.
    I noticed that when the PUT body is more than 1000 characters length, the error appears on android device and If body length is lass than 700 chars, the error never appears on android device. however It wont show on editor anyway at any circumstances.

    I also noticed that despite the error the server gets the complete PUT body! I mean even when android device throw the error, the server has the complete body and responds correctly.

    here is my code:

    Code (CSharp):
    1. void PUT(string strCorrectRequest, string body, Action<int, string> pmCallback)
    2.     {
    3.         UnityWebRequest www = UnityWebRequest.Put(strCorrectRequest, body);
    4.         www.timeout = 20;
    5.  
    6.         www.SetRequestHeader("Content-Type", "application/json");
    7.         yield return www.SendWebRequest();
    8.  
    9.         if (www.isNetworkError || www.isHttpError)
    10.         {
    11.             Debug.Log(www.responseCode.ToString());
    12.             Debug.Log(www.error);
    13.             pmCallback((int)www.responseCode, www.error + "\nReqUrl: " + strCorrectRequest + "\n");
    14.         }
    15.         else
    16.         {
    17.             Debug.Log(www.responseCode.ToString());
    18.             Debug.Log(www.downloadHandler.text);
    19.  
    20.              try
    21.              {
    22.               pmCallback((int)www.responseCode, www.downloadHandler.text);
    23.              }
    24.              catch (Exception ex)
    25.              {
    26.               Debug.LogError("PUT Call Back Error : " + ex.Message + "\nReqUrl: " + strCorrectRequest + "\n" + ex.StackTrace);
    27.              }
    28.         }
    29.     }
     
  8. bardia68

    bardia68

    Joined:
    Apr 1, 2016
    Posts:
    7
    I just found the solution: if you set
    www.chunkedTransfer = true;
    It works fine! hurraa! :)
     
    DaveMExient likes this.