[英]How can I make my android app connect to the internet? (Unity)
我正在做一個 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");
}
}
}
}
}
這就是組件的外觀:
(我已經嘗試將 html 更改為 https)
現在,當我在 Unity 編輯器上測試它時,它可以完美運行,但是當我在手機上嘗試它時,它就不行了。 文本永遠不會加載。
我認為這是關於權限的問題,所以我給了互聯網和 access_network_state,仍然無法正常工作(我在播放器設置中也需要互聯網)
我錯過了什么?
為了在您的應用程序中執行網絡操作,您的清單必須包含以下權限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.