'Can't connect to localhost in React Native WebView
I started up a simple http server (from here):
from wsgiref.simple_server import make_server
def app(environ, start_response):
start_response('200 OK', [('Content-Type','text/html')])
yield b"<h1>Goodbye, World!</h1>"
server = make_server('127.0.0.1', 8080, app)
server.serve_forever()
Ran it using:
python3 simple_serve.py
Redirecting to http://127.0.0.1:8080/ displays "Goodbye, World!".
I then created a new React Native Project and changed index.android.js to:
import React, { Component } from 'react';
import {
AppRegistry,
WebView,
} from 'react-native';
export default class WebTest extends Component {
render() {
return (
<WebView
source= {{ uri: "http://127.0.0.1:8080/" }}
/>
);
}
}
AppRegistry.registerComponent('WebTest', () => WebTest);
"Goodbye, World!" doesn't show up, and I get a warning that says:
Encountered an error loading page
{"canGoForward"false,"code":-6,"canGoBack":false,"description":"net::ERR_CONNECTION_REFUSED","loading":false,"title":"","url":"http://127.0.0.1:8080/","target":5}
For reference, changing http://127.0.0.1:8080/ to https://www.google.com works.
Solution 1:[1]
I had success doing :
adb reverse tcp:3000 tcp:3000
My webview:
<WebView source={{uri: 'http://localhost:3000'}} ...
Solution 2:[2]
Solution 3:[3]
use it like that http://10.0.2.2://your port num this will work definetly
Solution 4:[4]
I was able to connect to localhost by finding my computer's IP Address. Open cmd prompt and type this for mac:
ifconfig | grep inet
Windows:
ipconfig/ALL
mine was listed between inet & netmask exhttp://194.178.1.2:4242/ and as Webview uri worked fine
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Grégoire Lafortune |
| Solution 2 | mdehghani |
| Solution 3 | AKIN KARAYUN |
| Solution 4 | Blaze Gawlik |

