I am new to Android dev, I have started using Android Studio with Kotlin few days. I run into an issue today which is : how can my app communicate with my local server (XAMPP : Apache and MYSQL) I am running Windows 10, and using my real physical phone instead of the emulator (API 30) since the emulator works so slow for me.
I have a simple php app that only returns “hi”, I tried many ways to use AJAX requests but the one closest to working with is Android Volley, here is my code:
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)
at first I used my IP since we are connected to the same wifi (hotspot) and since I can actually open my localhost in my phone’s browser, but it didn’t work inside the code. (I had an error : java.net.MalformedURLException: no protocol
), I looked around and read that Volley doesn’t support IP addresses, so I used PAGEKITE to make my localhost accessible to the internet with a readable link, but now I am getting this error instead :
cleartext http traffic to not permitted
I added this android:usesCleartextTraffic="true"
to my manifest.xml and created 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>
But the issue is still the same. I tried the same thing with google.com also I tried my php file but in my webhosting, and it worked just fine, so the issue is somewhere in my localhost I would assume, probably because I am not using a secure connection (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.
Advertisement
Answer
Solved : I forgot to add my URL in network_security_config.xml
<domain includeSubdomains="true">192.168.56.172</domain>
Now it works with both my IP address or my URL.