賞金將在 2 天內到期。此問題的答案有資格獲得 50聲望賞金。 漠河夢幻想引起更多的關注。
我是 Android 開發新手,幾天前我已經開始使用帶有 Kotlin 的 Android Studio。我今天遇到了一個問題:我的應用程式如何與我的本地服務器(XAMPP:Apache 和 MYSQL)通信我正在運行 Windows 10,并使用我的真實物理手機而不是模擬器(API 30),因為模擬器作業如此對我來說很慢。
我有一個只回傳“hi”的簡單 php 應用程式,我嘗試了多種使用 AJAX 請求的方法,但最接近使用的是 Android Volley,這是我的代碼:
val queue = Volley.newRequestQueue(this)
val url = "http://mohe.pagekite.me"
lateinit var qq : String
val stringRequest = StringRequest(
Request.Method.GET, url,
{ response ->
qq = "Response is: ${response.substring(0, 500)}"
val toast = Toast.makeText(applicationContext, qq, Toast.LENGTH_LONG)
toast.show()
},
{ error-> qq = error.toString()
val toast = Toast.makeText(applicationContext, qq, Toast.LENGTH_LONG)
toast.show()
})
queue.add(stringRequest)
起初我使用了我的 IP,因為我們連接到同一個 wifi(熱點),而且我實際上可以在手機的瀏覽器中打開我的本地主機,但它在代碼中不起作用。(我有一個錯誤java.net.MalformedURLException: no protocol:),我環顧四周,發現 Volley 不支持 IP 地址,所以我使用 PAGEKITE 使我的本地主機可以通過可讀鏈接訪問互聯網,但現在我收到了這個錯誤:
cleartext http traffic to not permitted
我將此添加android:usesCleartextTraffic="true"到我的 manifest.xml 并創建了 network_security_config.xml :
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<base-config cleartextTrafficPermitted="true"/>
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</domain-config>
</network-security-config>
但問題還是一樣。我在 google.com 上嘗試了同樣的事情,我也嘗試了我的 php 檔案,但在我的虛擬主機中,它作業得很好,所以我認為問題出在我的本地主機中,可能是因為我沒有使用安全連接(https) ?
This bugged me because how else would I be able to make an app with a database if I can't comminute with my localhost ? using my real webhost slows the development process and I can't get my head around this issue. How do you develop and test your app with your local server or am I doing everything wrong ?
Edit : the purpose of my app is to be able to login and read/edit some data from any device.
uj5u.com熱心網友回復:
已解決:我忘記添加我的網址network_security_config.xml
<domain includeSubdomains="true">192.168.56.172</domain>
現在它適用于我的 IP 地址或我的 URL。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/449504.html
