【问题标题】:How can I make my android app connect to the internet? (Unity)如何让我的 android 应用程序连接到互联网? (统一)
【发布时间】:2020-08-04 22:09:38
【问题描述】:

我正在做一个 Android 应用程序,它从网站请求信息(我已经创建了网站)并将其显示在屏幕上。

我有这样的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Android;
using UnityEngine.Networking;
using UnityEngine.UI;

public class WebTextLoader : MonoBehaviour
{
    // Start is called before the first frame update
    [Serializable]
    public class PlaceInfo
    {
        public string Titulo = "";
        public string Texto = "";
    }

    public string URL;
    public Text TituloUI;
    public Text TextoUI;

    public PlaceInfo placeInfo;



    public void Start()
    {
        if (Debug.isDebugBuild)
        {
            StartCoroutine(GetRequest(URL));

        }
    }
    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            // Request and wait for the desired page.
            yield return webRequest.SendWebRequest();

            string jsonForm = uri;

            if (webRequest.isNetworkError)
            {
                Debug.Log("Error loading");

            }
            else
            {
                try
                {
                    placeInfo = JsonUtility.FromJson<PlaceInfo>(webRequest.downloadHandler.text);
                    TituloUI.text = placeInfo.Titulo;
                    TextoUI.text = placeInfo.Texto;

                }
                catch
                {
                    Debug.Log("Error in connection");
                }
            }

        }
    }
}

这就是组件的外观:

picture of the component

(我已经尝试将 HTTPS 更改为 HTTPS)

现在,当我在 Unity 编辑器上测试它时,它可以完美运行,但是当我在手机上尝试它时,它却不行。文本永远不会加载。

我以为是权限的问题,所以我给了 internet 和 access_network_state,仍然没有工作(我在播放器设置中也需要 internet)

我错过了什么?

【问题讨论】:

    标签: c# android unity3d web


    【解决方案1】:

    为了在您的应用程序中执行网络操作,您的清单必须包含以下权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

    • 我想你忘了写权限
    • 大声笑,它们被保存为链接,我已经更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    • 2011-02-19
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多