React Native App crashes on Network error
I am developing one document upload app, using react native.
Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.
I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?
Below is my code -
try{
var form = FormData();
form.append("user_id",this.state.userId);
form.append('file', {
uri: this.state.path,
type: 'image/jpg',
name: 'image.jpg',
});
axios({
method:"POST",
url:"http://xxxxx/xxxx",
timeout:50000,
data:form
})
.then(response => console.log(response))
.catch(error=>console.log(error));
}
catch(error){
console.log(error);
ToastAndroid.show("Internal error, please try again");
}
Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.
Also have added all permissions in Manifest file for internet access.
Please help
add a comment |
I am developing one document upload app, using react native.
Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.
I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?
Below is my code -
try{
var form = FormData();
form.append("user_id",this.state.userId);
form.append('file', {
uri: this.state.path,
type: 'image/jpg',
name: 'image.jpg',
});
axios({
method:"POST",
url:"http://xxxxx/xxxx",
timeout:50000,
data:form
})
.then(response => console.log(response))
.catch(error=>console.log(error));
}
catch(error){
console.log(error);
ToastAndroid.show("Internal error, please try again");
}
Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.
Also have added all permissions in Manifest file for internet access.
Please help
add a comment |
I am developing one document upload app, using react native.
Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.
I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?
Below is my code -
try{
var form = FormData();
form.append("user_id",this.state.userId);
form.append('file', {
uri: this.state.path,
type: 'image/jpg',
name: 'image.jpg',
});
axios({
method:"POST",
url:"http://xxxxx/xxxx",
timeout:50000,
data:form
})
.then(response => console.log(response))
.catch(error=>console.log(error));
}
catch(error){
console.log(error);
ToastAndroid.show("Internal error, please try again");
}
Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.
Also have added all permissions in Manifest file for internet access.
Please help
I am developing one document upload app, using react native.
Documents get uploaded if the network is good, but suddenly the app crashes if the network goes down.
For instance - If I am uploading the documents on 4G network then i receive success from API but if the network speed suddenly goes down to 2G network i.e. 20KB/S then I get "NETWORK REQUEST FAILED / NETWORK ERROR" and the app crashes.
I have handled the error using catch block, but still the app crashes, what should I do to avoid this crashing even if the network is not available or if the speed goes down ?
Below is my code -
try{
var form = FormData();
form.append("user_id",this.state.userId);
form.append('file', {
uri: this.state.path,
type: 'image/jpg',
name: 'image.jpg',
});
axios({
method:"POST",
url:"http://xxxxx/xxxx",
timeout:50000,
data:form
})
.then(response => console.log(response))
.catch(error=>console.log(error));
}
catch(error){
console.log(error);
ToastAndroid.show("Internal error, please try again");
}
Note - I have used react-native-image-picker, as the document can be captured using camera OR it could be uploaded from the existing file present in the device.
Also have added all permissions in Manifest file for internet access.
Please help
edited Nov 24 '18 at 8:51
Krunal Panchal
asked Nov 24 '18 at 8:39
Krunal PanchalKrunal Panchal
64
64
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Thats an Exception. I guess you need to catch the Exception
BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
In the Catch You need to Show the Dialog box which says Can't connect to server.
OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
Hope that helps .
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456551%2freact-native-app-crashes-on-network-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thats an Exception. I guess you need to catch the Exception
BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
In the Catch You need to Show the Dialog box which says Can't connect to server.
OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
Hope that helps .
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
add a comment |
Thats an Exception. I guess you need to catch the Exception
BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
In the Catch You need to Show the Dialog box which says Can't connect to server.
OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
Hope that helps .
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
add a comment |
Thats an Exception. I guess you need to catch the Exception
BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
In the Catch You need to Show the Dialog box which says Can't connect to server.
OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
Hope that helps .
Thats an Exception. I guess you need to catch the Exception
BadNetwork or Timeout You need to Catch it and App should not Crash just make sure
In the Catch You need to Show the Dialog box which says Can't connect to server.
OR You need to check if you have a Network connection then only Let the user send the Request show Non-cancelable Dialogbox
Hope that helps .
answered Nov 24 '18 at 8:49
i_fahimkhani_fahimkhan
12
12
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
add a comment |
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
I've already added try catch block and axios also has an catch block, but I am not able to get even the console.log(), and how can i prevent crashing ? i.e. app should not crash and stay as it is with an error message in toast ? Please help
– Krunal Panchal
Nov 24 '18 at 9:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456551%2freact-native-app-crashes-on-network-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown