[英]Cannot connect android React Native app to localHost .NET api
I have a React Native app that simply needs to login and subsequently fetch data using the returned access_token. 我有一个React Native应用程序,只需要登录并随后使用返回的access_token获取数据。 I am trying to debug the error by connecting to the locally hosted .NET api running on localHost:50968. 我正在尝试通过连接到在localHost:50968上运行的本地托管.NET api来调试错误。 This returns 这返回
TypeError: Network request failed
I have already enabled the internet permission in the manifext.xml. 我已经在manifext.xml中启用了Internet权限。 Most of the googling has been saying that i need to change localHost:50968 to my ip address.This has been the closest so far using '192.168.0.2:50968/token but the below is the response. 大部分谷歌搜索都在说我需要将localHost:50968更改为我的IP地址。这是到目前为止使用'192.168.0.2:50968/token进行的更新,但是下面是响应。
{ type: 'default',
status: 400,
ok: false,
statusText: undefined,
headers: { map: { 'content-length': [ '334'
],
connection: [ 'close'
],
date: [ 'Thu,
10 Aug201703: 19: 14 GMT'
],
server: [ 'Microsoft-HTTPAPI/2.0'
],
'content-type': [ 'text/html; charset=us-ascii'
]
}
}, url: 'http: //192.168.0.2:50968/token',
_bodyInit:
'<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">\r\n
<HTML>
<HEAD>
<TITLE>Bad Request</TITLE>\r\n
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
</HEAD>\r\n
<BODY>
<h2>Bad Request - Invalid Hostname</h2>\r\n
<hr>
<p>HTTP Error 400. The request hostname is invalid.</p>\r\n
</BODY>
</HTML>\r\n', _bodyText: '
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> \r\n
<HTML>
<HEAD>
<TITLE>Bad Request
</TITLE>\r\n
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii">
</HEAD>\r\n
<BODY>
<h2>Bad Request - Invalid Hostname</h2>\r\n
<hr>
<p>HTTP Error 400. The request hostname is invalid.</p>\r\n
</BODY>
</HTML>\r\n'}
I have a breakpoint in Visual Studio waiting for the request but I cannot for the life of me get this to work. 我在Visual Studio中有一个断点在等待请求,但是我无法一辈子让它正常工作。 My code for the fetch is as follows. 我的提取代码如下。
Iv tried the these as the URL_LOGIN and neither work. iv尝试了这些作为URL_LOGIN,但都没有用。 ' http://localHost:50968/token '; ' http:// localHost:50968 / token '; ' http://192.168.0.2:50968/token '; ' http://192.168.0.2:50968/token ';
fetch(URL_LOGIN, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
//application/x-www-form-urlencoded
},
body: data
}).then((response) => {
var jsonResponse = JSON.parse(response._bodyInit);
try {
AsyncStorage.setItem('token', jsonResponse.access_token);
loginUserSuccess(dispatch, response)
} catch (error) {
console.log(`Error while saving to Async Storage ${error.message}`)
loginUserFailed(dispatch, error)
}
}).catch(response => {
loginUserFailed(dispatch, response)
});
Any help would be GREATLY appreciated. 任何帮助将不胜感激。
Ended up finding this. 最终找到了这个。 Just in case anybody else has this issue. 以防万一其他人有这个问题。
http://briannoyesblog.azurewebsites.net/2016/03/06/calling-localhost-web-apis-from-visual-studio-android-emulator/ http://briannoyesblog.azurewebsites.net/2016/03/06/calling-localhost-web-apis-from-visual-studio-android-emulator/
The best way I found was actually to use this 我发现的最好方法实际上是使用此方法
https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti
Saved my behind. 救了我的身后。
Use 10.0.2.2
instead of localhost
使用10.0.2.2
而不是localhost
http://10.0.2.2:50968/token
On android localhost maps to the emulator and not your machine. 在android上,localhost映射到仿真器而不是您的计算机。
Hope this helps 希望这可以帮助
In the response you've posted, I noticed it says url: 'http: //192.168.0.2:50968/token'
, with a space between http:
and //
. 在您发布的回复中,我注意到它说的是url: 'http: //192.168.0.2:50968/token'
,在http:
和//
之间有一个空格。 Are you sure you haven't just incorrectly formatted your URL in URL_LOGIN
? 您确定不只是在URL_LOGIN
错误地格式化了URL吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.