英文:
How can i connect my database to my android app
问题
以下是你提供的内容的翻译:
所以,我对数据库还是很新的,仅在几天前学习了MySQL,然而我正在尝试连接由000webhost.com托管的数据库。我已经创建了连接并从数据库获取数据并将其转换为JSON的PHP文件,但无论何时我尝试从我的应用程序访问PHP文件,都会显示错误。
我的代码:
protected String doInBackground(String[] p1) {
try {
URL url = new URL("https://example.000webhostapp.com/Export.php");
URLConnection urlconnection = url.openConnection();
InputStreamReader inputstream = new InputStreamReader(urlconnection.getInputStream());
BufferedReader reader = new BufferedReader(inputstream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
错误信息:
04-04 07:38:25.222 20067 20090 W System.err java.net.ConnectException: 无法连接到example.000webhostapp.com/145.14.144.173:443
04-04 07:38:25.225 20067 20090 W System.err at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:1417)
...
PHP文件:
<?php
require("connect.php");
$sql = "select * from Med";
$result = $conn->query($sql);
$array = array();
while ($raw = $result->fetch_assoc()) {
array_push($array, $raw);
}
echo json_encode($array);
?>
编辑:我甚至尝试将URL替换为www.google.com,但仍显示相同的错误。
英文:
So I'm pretty new to databases i only learned mysql few days ago however i'm trying to connect to a database hosted by 000webhost.com I've created the php file to connect and get data from database and convert it to json but whenever i try to access the php file from my app it shows an error.
My code :
protected String doInBackground(String[] p1)
{
try
{
URL url = new URL("https://example.000webhostapp.com/Export.php");
URLConnection urlconnection = url.openConnection();
InputStreamReader inputstream = new InputStreamReader(urlconnection.getInputStream());
BufferedReader reader = new BufferedReader(inputstream);
}
catch (MalformedURLException e)
{e.printStackTrace();}
catch (IOException e)
{e.printStackTrace();}
return null;
}
The error :
04-04 07:38:25.222 20067 20090 W System.err java.net.ConnectException: Failed to connect to example.000webhostapp.com/145.14.144.173:443
04-04 07:38:25.225 20067 20090 W System.err at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:1417)
04-04 07:38:25.226 20067 20090 W System.err at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:1367)
04-04 07:38:25.227 20067 20090 W System.err at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:219)
04-04 07:38:25.227 20067 20090 W System.err at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:142)
04-04 07:38:25.228 20067 20090 W System.err at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:104)
04-04 07:38:25.229 20067 20090 W System.err at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:392)
04-04 07:38:25.230 20067 20090 W System.err at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:325)
04-04 07:38:25.231 20067 20090 W System.err at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:489)
04-04 07:38:25.231 20067 20090 W System.err at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:435)
04-04 07:38:25.232 20067 20090 W System.err at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)
04-04 07:38:25.233 20067 20090 W System.err at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
04-04 07:38:25.234 20067 20090 W System.err at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(Unknown Source:0)
04-04 07:38:25.235 20067 20090 W System.err at com.mycompany.database.MainActivity$connect.doInBackground(MainActivity.java:42)
04-04 07:38:25.236 20067 20090 W System.err at com.mycompany.database.MainActivity$connect.doInBackground(Unknown Source:6)
04-04 07:38:25.236 20067 20090 W System.err at android.os.AsyncTask$2.call(AsyncTask.java:333)
04-04 07:38:25.237 20067 20090 W System.err at java.util.concurrent.FutureTask.run(FutureTask.java:266)
04-04 07:38:25.238 20067 20090 W System.err at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
04-04 07:38:25.238 20067 20090 W System.err at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
04-04 07:38:25.239 20067 20090 W System.err at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
04-04 07:38:25.240 20067 20090 W System.err at java.lang.Thread.run(Thread.java:764)
The php file
<?php
require("connect.php");
$sql = "select * from Med";
$result = $conn->query($sql);
$array = array();
while ($raw = $result->fetch_assoc()) {
array_push($array,$raw);
}
echo json_encode($array)
?>
EDIT : I even tryed to replace the url with www.google.com and it shows the same error.
答案1
得分: 0
抱歉我太傻了,我忘记了我有一个防火墙应用,它禁止新应用连接到互联网,非常抱歉浪费了你的时间。
英文:
Omg I'm so stupid I forgot that I have a firewall app that forbids new apps from connecting to internet I'm very sorry for wasting your time.
专注分享java语言的经验与见解,让所有开发者获益!
评论