Search Unity

WebClient doesn't work in apk when I try to read a text from file.txt which is on a server

Discussion in 'Scripting' started by unity_6BFF19D91BC7381033E2, Nov 8, 2022.

  1. unity_6BFF19D91BC7381033E2

    unity_6BFF19D91BC7381033E2

    Joined:
    Jun 12, 2022
    Posts:
    10
    Good evening. I would like to read a text from a txt-file which is on a server (like https://example-files.online-convert.com/document/txt/example.txt). In Editor it works correctly, but in a build for Android it throws an exception. I have also put "required" instead of "auto" in player settings.

    I have two scripts, the script ShowingTheTextFromASevrer is hung on the Main Camera. Also there is a Text (TextMeshProUGUI) on the screen. In a build I see "An exception occured during a WebClient request" on the screen.

    How to fix it?

    Code (CSharp):
    1. using System.Net;
    2. using System.IO;
    3. using UnityEngine;
    4.  
    5. public abstract class GettingTextFromAServer : MonoBehaviour
    6. {
    7.    public string GetTheTextFromAServer(string url)
    8.    {
    9.        WebClient client = new WebClient();
    10.        Stream stream = client.OpenRead(url);
    11.        StreamReader reader = new StreamReader(stream);
    12.        return reader.ReadToEnd();
    13.    }
    14. }
    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class ShowingTheTextFromASevrer : GettingTextFromAServer
    5. {
    6.    [SerializeField] private TextMeshProUGUI TextOnTheScreen;
    7.  
    8.    private void Start()
    9.    {
    10.        TextOnTheScreen.text = "";
    11.        string TextFromTheServer;
    12.        try
    13.        {
    14.            TextFromTheServer = GetTheTextFromAServer("https://example-files.online-convert.com/document/txt/example.txt");
    15.            Debug.Log(TextFromTheServer);
    16.            TextOnTheScreen.text += TextFromTheServer;
    17.        }
    18.        catch (System.Exception AnyException)
    19.        {
    20.            TextOnTheScreen.text += AnyException.Message;
    21.        }
    22.    }
    23. }
    Maybe, I should modify the mainfest. I have found it, it is:

    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest
    3.    xmlns:android="http://schemas.android.com/apk/res/android"
    4.    package="com.unity3d.player"
    5.    xmlns:tools="http://schemas.android.com/tools">
    6.    <application>
    7.        <activity android:name="com.unity3d.player.UnityPlayerActivity"
    8.                  android:theme="@style/UnityThemeSelector">
    9.            <intent-filter>
    10.                <action android:name="android.intent.action.MAIN" />
    11.                <category android:name="android.intent.category.LAUNCHER" />
    12.            </intent-filter>
    13.            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    14.        </activity>
    15.    </application>
    16. </manifest>
    Where should I put

    Code (csharp):
    1. <uses-permission android:name="android.permission.INTERNET"/>
    2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    or analogical?

    UPD-2:
    I have solved that.
    1) In the manifest
    Code (csharp):
    1. <permission android:name="android.permission.INTERNET"/>
    2. <permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    should be put above the "<application>" tag.
    2) The app should be signed with keycode (like we do before the publication to Play Market) to work.
     
    Last edited: Nov 13, 2022