我是Android Studio开发的新手,但是,我在Unity3D C#开发方面经验丰富。我有一个PHP脚本,它返回一些文本数据,并将其托管在000webhost上。我可以使用UnityWebRequest
类及其downloadHandler.text
使用Unity3D C#脚本检索该数据。
现在,我正在Android Studio Java中进行尝试。问题是我无法从PHP脚本获取数据。我用不同的URL尝试了相同的代码,并且可以正常工作。我尝试将URL更改为“ www.bing.com”,它显示了该Bing网页的数据。它仅不能从000webhost获取数据。请告诉我Android Studio(Java)和000webhost的问题是什么。
这是我的代码:
@Override protected Void doInBackground(Void... voids)
{
try
{
URL url = new URL("https://000webhost_URL"); // www.bing.com works
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = br.readLine();
data = data + line;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
Log.d(tag,"Fetched data: " + data);
}
/*
I get empty Data when it is fetched from 000webhost
Fetched data:
When I try from bing.com I get the response stream:
Fetched Data: <!DOCTYPE html><html lang="en"><head><meta content="text/html; /....
*/