[英]How can I make my android app connect to the internet? (Unity)
I'm doing an Android application that request info from a website (I already made the website) and displays it on the screen.我正在做一个 Android 应用程序,它从网站请求信息(我已经创建了网站)并将其显示在屏幕上。
I have this code to do that:我有这个代码来做到这一点:
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");
}
}
}
}
}
And this is how the component looks:这就是组件的外观:
(I already tried changing htttp to https) (我已经尝试将 html 更改为 https)
Now, when I test it on the Unity editor, it works perfectly, but when I try it on my phone, it doesn't.现在,当我在 Unity 编辑器上测试它时,它可以完美运行,但是当我在手机上尝试它时,它就不行了。 The text never loads.文本永远不会加载。
I thought it was something about the permissions, so I gave the internet and the access_network_state, still didn't work (I also have the internet on require in the player settings)我认为这是关于权限的问题,所以我给了互联网和 access_network_state,仍然无法正常工作(我在播放器设置中也需要互联网)
I'm missing something?我错过了什么?
In order to perform network operations in your application, your manifest must include the following permissions:为了在您的应用程序中执行网络操作,您的清单必须包含以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.