Android 中的 https 连接 react-native 在构建后无法在物理设备上运行
https connection in Android react-native not working on physical device after build
我使用 expo 和 Axios(用于服务器请求)创建了一个带有 react-native 的简单应用程序
在构建之前,在开发过程中,所有 https 请求都工作正常。
构建后,当运行物理设备上的apk时,https根本不起作用。
我在 Logcat 中遇到的错误是“网络错误”。
应用程序中的其他 Internet 连接(构建后)可以正常工作,例如 webview 打开网页或 Firebase 连接。
analyzerApi.post('/analyze', urls) .then((res) => {
dispatch({type: 'get_result', payload: res.data.analysis})}).catch(err => console.log("Error in getting analyze.. " ,err.name, err.message))
(analyzerApi 是一个 axios 实例,baseUrl 指向我的服务器)
此调用将适用于 HTTP 和 HTTPS 尝试此示例 POST CALL
fetch('http://mywebsite.com/endpoint/', {
method: "POST",
headers: {
// Authorization: "Bearer " + Token,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
// sending data userID username, password, etc
}),
})
.then((response) => response.json())
.then((responseJson) => {
// Response will here
})
.catch((error) => {
alert(error);
});
试试这个 GET CALL 示例
fetch('http://mywebsite.com/endpoint/', {
method: "GET",
headers: {
// Authorization: "Bearer " + Token,
// OR USER name PASSworrd
Accept: 'application/json',
'Content-Type': 'application/json'
},
})
.then((response) => response.json())
.then((responseJson) => {
// Response will here
})
.catch((error) => {
alert(error);
});
我使用 expo 和 Axios(用于服务器请求)创建了一个带有 react-native 的简单应用程序
在构建之前,在开发过程中,所有 https 请求都工作正常。
构建后,当运行物理设备上的apk时,https根本不起作用。
我在 Logcat 中遇到的错误是“网络错误”。 应用程序中的其他 Internet 连接(构建后)可以正常工作,例如 webview 打开网页或 Firebase 连接。
analyzerApi.post('/analyze', urls) .then((res) => {
dispatch({type: 'get_result', payload: res.data.analysis})}).catch(err => console.log("Error in getting analyze.. " ,err.name, err.message))
(analyzerApi 是一个 axios 实例,baseUrl 指向我的服务器)
此调用将适用于 HTTP 和 HTTPS 尝试此示例 POST CALL
fetch('http://mywebsite.com/endpoint/', {
method: "POST",
headers: {
// Authorization: "Bearer " + Token,
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
// sending data userID username, password, etc
}),
})
.then((response) => response.json())
.then((responseJson) => {
// Response will here
})
.catch((error) => {
alert(error);
});
试试这个 GET CALL 示例
fetch('http://mywebsite.com/endpoint/', {
method: "GET",
headers: {
// Authorization: "Bearer " + Token,
// OR USER name PASSworrd
Accept: 'application/json',
'Content-Type': 'application/json'
},
})
.then((response) => response.json())
.then((responseJson) => {
// Response will here
})
.catch((error) => {
alert(error);
});