Android React Native未找到我的localhost服务器(Genymotion和Expo)

时间:2018-03-30 16:24:17

标签: reactjs typescript react-native genymotion expo

注意此代码适用于iOS但不适用于Android

let data = fetch('http://mylocalapi.test/api/query?resource=article')
.then((response) => {
  console.log("Get Data");
  console.log(response);
  this.setState({articles:JSON.parse(response._bodyInit)});
});

响应返回以下内容。它似乎试图将我重定向到dnssearch.com,因为它无法找到我的rul。我的网址是一个实际的测试域。

我使用genymotion作为模拟器并使用Expo

进行测试
12:16:23: Response {
12:16:23:   "_bodyInit": "<html><head><meta http-equiv=\"refresh\" content=\"0;url=http://www.dnsrsearch.com/index.php?origURL=http://whatshapp.test/api/query&bc=\"/></head><body><script type=\"text/javascript\">window.location=\"http://www.dnsrsearch.com/index.php?origURL=\"+escape(window.location)+\"&r=\"+escape(document.referrer)+\"&bc=\";</script></body></html>",
12:16:23:   "_bodyText": "<html><head><meta http-equiv=\"refresh\" content=\"0;url=http://www.dnsrsearch.com/index.php?origURL=http://whatshapp.test/api/query&bc=\"/></head><body><script type=\"text/javascript\">window.location=\"http://www.dnsrsearch.com/index.php?origURL=\"+escape(window.location)+\"&r=\"+escape(document.referrer)+\"&bc=\";</script></body></html>",
12:16:23:   "headers": Headers {
12:16:23:     "map": Object {
12:16:23:       "cache-control": Array [
12:16:23:         "public, max-age=0",
12:16:23:       ],
12:16:23:       "connection": Array [
12:16:23:         "close",
12:16:23:       ],
12:16:23:       "content-type": Array [
12:16:23:         "text/html",
12:16:23:       ],
12:16:23:       "date": Array [
12:16:23:         "Fri, 30 Mar 2018 16:15:49 GMT",
12:16:23:       ],
12:16:23:       "expires": Array [
12:16:23:         "Fri, 30 Mar 2018 16:15:48 GMT",
12:16:23:       ],
12:16:23:       "server": Array [
12:16:23:         "nginx",
12:16:23:       ],
12:16:23:       "transfer-encoding": Array [
12:16:23:         "chunked",
12:16:23:       ],
12:16:23:       "vary": Array [
12:16:23:         "Accept-Encoding",
12:16:23:       ],
12:16:23:     },
12:16:23:   },
12:16:23:   "ok": true,
12:16:23:   "status": 200,
12:16:23:   "statusText": undefined,
12:16:23:   "type": "default",
12:16:23:   "url": "http://whatshapp.test/api/query?resource=article",
12:16:23: }

1 个答案:

答案 0 :(得分:0)

我在ios工作之前遇到过这个问题,但是在使用localhost或0.0.0.0时没有使用android。对我有用的解决方案是使用我的内部ip,如here

所述