How to access the local host server with web view in Android
Solution 1
Make sure you are not using "localhost" from you app (a very common mistake).
If you are accessing your local server from a wifi, you just need to check which IP your server has assigned and use it in the app.
If you are using the emulator, try with 10.0.2.2
See here Accessing localhost:port from Android emulator
Solution 2
In your activity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// you should be to put localhost and port number
//webView.loadUrl("http://local_host:<port-number>/index.html")
//example
webView.loadUrl("http://10.66.0.89:8080/index.html")
webView.settings.cacheMode = WebSettings.LOAD_NO_CACHE
webView.settings.setAppCacheEnabled(false)
}
}
in your manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<application
...
android:usesCleartextTraffic="true">
Solution 3
I too had the same issue. Let's say the IP address is `216.58.216.164. I rectified it using following
webView.loadUrl("http://216.58.216.164/")
Admin
Updated on June 06, 2022Comments
-
Admin almost 2 years
I have an Android Application and I need to go to my web server and connect my Android Application to another program on the web. my web program is working and when I put its IP address on Android FIrefox or Browser it is working but when I call its IP address with my webview ( webView.loadUrl(my web program IP address) it's not working. Can somebody help me where is my mistake?
-
hatzaviv almost 7 yearsYou should also make sure that your firewall isn't blocking your access.