问题说明::
我正在从事本机应用程序。我的服务器中的API数据太多。如果我是在页面上第一次点击此API,那么它工作正常。但是应用程序在同一页面上第二次点击API时关闭。在Firebase上没有错误/崩溃日志。
我正在使用axios来访问服务器上的API。
代码::
componentDidMount() {
this.getSingleUser();
}
async getSingleUser() {
NetInfo.isConnected.fetch().then(isConnected => {
axios.get(`${api_URL}`)
.then(response => {
Toast.show("2");
this.checkApiResponse(response.data.response);
})
.catch(error => {
Toast.show(JSON.stringify(error));
});
});
}
checkApiResponse(response) {
this.setState({ loading: false, refreshing: false });
console.log(response);
}
错误日志::
--------- beginning of system
2019-03-20 15:06:37.846 555-555/? E/lowmemorykiller: Error writing /proc/23858/oom_score_adj; errno=22
2019-03-20 15:06:37.846 555-555/? E/lowmemorykiller: Error writing /proc/23825/oom_score_adj; errno=22
2019-03-20 15:06:37.847 555-555/? E/lowmemorykiller: Error writing /proc/22442/oom_score_adj; errno=22
2019-03-20 15:06:37.856 555-555/? E/lowmemorykiller: Error writing /proc/23825/oom_score_adj; errno=22
2019-03-20 15:06:37.885 555-555/? E/lowmemorykiller: Error writing /proc/22442/oom_score_adj; errno=22
2019-03-20 15:06:37.924 555-555/? E/lowmemorykiller: Error writing /proc/23825/oom_score_adj; errno=22
2019-03-20 15:06:37.924 555-555/? E/lowmemorykiller: Error writing /proc/22442/oom_score_adj; errno=22
2019-03-20 15:06:37.973 555-555/? E/lowmemorykiller: Error writing /proc/23825/oom_score_adj; errno=22
2019-03-20 15:06:37.987 555-555/? E/lowmemorykiller: Error opening /proc/22442/oom_score_adj; errno=2
2019-03-20 15:06:37.989 555-555/? E/lowmemorykiller: Error writing /proc/23107/oom_score_adj; errno=22
2019-03-20 15:06:37.989 555-555/? E/lowmemorykiller: Error writing /proc/22296/oom_score_adj; errno=22
2019-03-20 15:06:37.989 555-555/? E/lowmemorykiller: Error writing /proc/24137/oom_score_adj; errno=22
2019-03-20 15:06:37.990 555-555/? E/lowmemorykiller: Error writing /proc/23642/oom_score_adj; errno=22
2019-03-20 15:06:37.990 555-555/? E/lowmemorykiller: Error opening /proc/23825/oom_score_adj; errno=2
2019-03-20 15:06:38.009 555-555/? E/lowmemorykiller: Error writing /proc/23107/oom_score_adj; errno=22
2019-03-20 15:06:38.009 555-555/? E/lowmemorykiller: Error writing /proc/22296/oom_score_adj; errno=22
2019-03-20 15:06:38.010 555-555/? E/lowmemorykiller: Error writing /proc/23642/oom_score_adj; errno=22
2019-03-20 15:06:38.020 555-555/? E/lowmemorykiller: Error writing /proc/23107/oom_score_adj; errno=22
2019-03-20 15:06:38.020 555-555/? E/lowmemorykiller: Error writing /proc/23579/oom_score_adj; errno=22
2019-03-20 15:06:38.021 555-555/? E/lowmemorykiller: Error writing /proc/24137/oom_score_adj; errno=22
2019-03-20 15:06:38.021 555-555/? E/lowmemorykiller: Error writing /proc/23642/oom_score_adj; errno=22
2019-03-20 15:06:38.045 555-555/? E/lowmemorykiller: Error opening /proc/23579/oom_score_adj; errno=2
2019-03-20 15:06:38.723 555-555/? E/lowmemorykiller: Error writing /proc/23148/oom_score_adj; errno=22
2019-03-20 15:06:38.726 555-555/? E/lowmemorykiller: Error writing /proc/19742/oom_score_adj; errno=22
2019-03-20 15:06:38.731 555-555/? E/lowmemorykiller: Error writing /proc/23148/oom_score_adj; errno=22
2019-03-20 15:06:38.760 1588-1800/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=632, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
2019-03-20 15:06:38.765 555-555/? E/lowmemorykiller: Error writing /proc/22716/oom_score_adj; errno=22
2019-03-20 15:06:38.786 743-743/? E/cnss-daemon: Stale or unreachable neighbors, ndm state: 4
2019-03-20 15:06:38.817 1588-1800/? E/ConnectivityService: RemoteException caught trying to send a callback msg for NetworkRequest [ id=627, legacyType=-1, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED] ]
2019-03-20 15:06:38.850 1588-1779/? E/InputDispatcher: channel '347cfde com.miui.home/com.miui.home.launcher.Launcher (server)' ~ Channel is unrecoverably broken and will be disposed!
2019-03-20 15:06:39.579 24915-24915/? E/miuisdk: got ModuleLoadException when load modules: no such module found: com.miui.personalassistant
2019-03-20 15:06:39.820 743-743/? E/cnss-daemon: Stale or unreachable neighbors, ndm state: 4
2019-03-20 15:06:40.366 24931-23888/? A/google-breakpad: Microdump skipped (uninteresting)
如果API数据太小,有时可以正常工作。 API数据太大时,这是一个问题。请提出建议,如果有人有解决方案?
答案 0 :(得分:0)
我认为这是由于从api获得响应之前正在调用该函数,因为如您提到的那样,它无法正常工作,并且无法在您的代码中看到await
。尝试将await
添加到您的axios
中并进行检查。希望对您有帮助。
答案 1 :(得分:0)
正如@Maneesh所说,这可能是由于多次调用函数而没有等待响应引起的。 您提到您已经向Axios添加了超时,但是有一个known issue,仅向Axios添加超时是行不通的。 (我过去曾经遇到过这个问题)
有一种解决方法可以为请求设置超时-使用cancelToken
和普通的JavaScript setTimeout()
手动中止请求
function setRequestTimeout(source, timeout) {
setTimeout(() => {
source.cancel('timeout')
}, timeout);
}
function getRequest() {
return new Promise((resolve, reject) => {
const timeout = 10000
let source = axios.CancelToken.source()
setRequestTimeout(source, timeout)
try {
const response = await axios.get('/your-address/', {
timeout: timeout,
headers: {
'Content-type': 'application/json'
},
cancelToken: source.token
})
resolve(response)
} catch (error) {
reject(error)
}
})
}
过去这对我有用,我的解决方案来自我之前提到的this问题中的评论,希望对您有所帮助!