I have written an Android application in which I allow the user to select an image from the gallery and post it to an Internet server as a Base64 encoded String using HTTP REST-ful web-services upon click of a button.
When the server is up/ running and the phone has a good 3G connection, it works fine. However, if the phone has a bad connection or if the server is down for some reason, then the Android application crashes altogether (screen goes black and application quits).
Although I’ve given a catch block to catch any IO/ other exceptions, the catch block code doesn’t execute if there is an error while executing connection.connect () ; I have tried catching Throwable as well to no avail – the code in the catch block never executes and the application itself stops working.
Can somebody please help me in figuring out how to catch these exceptions which are occurring when the server is not reachable or if there is an error while transmitting the image?
if you check (ex as Exception) instead of IOException, maybe it helps
Related
I have an app for android created with kotlin.
In general, it works fine. But one user reports he can not login and error is like no internet connection.
However, he says connection is fine. He can open api endpoint with the browser on same device .
What can be the reason for this?
The app has manifest record for network permissions. For other users it works fine
As you don't have given much information, I can give you some points to check:
Network permission in manifest file (that is already done).
Run time permission for Android 6.0 and above.
check if you done something like this code.
try{
//network code
}
catch(Exception e){
DialogBOXSHOW("Network error");
}
because Exception here can catch any type of error even NullPointerException
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
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 .
While I am test-driving my unpublished application on my physical device without connecting it to a computer, sometimes an exception happens and the application gets killed. Now, if I connect the device to my computer and launch Android Studio and connect the Android monitor, the logcat does not show the exception that happened before connecting the device, and the exception is not reproducible.
Is there any way to make the device keep logs so that I can check it later with Android Studio?
Check this out : https://github.com/gaozp/ErrorHandleDemo ;
or you can check out this : http://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler.html;
BTW this means you should implement a handler to catch all exception that your app occurs . and when your app has hit a exception ,this handler will catch this ,and you can save it to a time-named log to record this exception.
I try to create BluetoothServerSocket on Samsung Galaxy Gio.
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp = null;
try
{
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_INSECURE, MY_UUID_INSECURE);
}
catch (IOException e)
{
}
mmServerSocket = tmp;
When I no longer need to use a socket I just close it.
public void cancel()
{
try
{
mmServerSocket.close();
}
catch (IOException e)
{
}
}
In both cases no exceptions are throws. So my problem is. When i try to use 1st pease of code again( without exit from app) Log cat show me exception :
07-07 18:27:44.239: D/BluetoothSocket(13672): create BluetoothSocket: type = 1, fd =-1,
uuid = [null], port = 25
07-07 18:27:44.339: E/BLZ20_WRAPPER(13672): ##### ERROR : __listen_prot_rfcomm: failed
with reason 1#####
That happens till then i don't reboot my phone or TurnOFF\wait\TurnOn my bluetooth. So i think the problem is that BluetoothServerSocket create sockets but he don't close it. Maybe my preconceptions is not right so i want to help.
After a bit of searching, I found this thread whose poster appears to get the same exception (Although it's in Italian, so I don't really understand...). If you scroll down a little, you'll see a java exception thrown for this native exception (__listen_prot_rfcomm: failed with reason 1). The java exception is java.io.IOException: Bad file number.
You can find this problem in many threads online (Android Bluetooth IOException bad file number, Bluetooth failed to get port number). From these 2 we can learn that this problem is device specific. Some devices seem to keep the file descriptor of the socket alive, even after you close it with BluetoothServerSocket.close(), so you can't recreate any sockets using the same settings.
The solution will depend on your application audience:
If you are not planning to publish it in the Play Store/Any other market, you could call BluetoothAdapter.disable(), and then re-enable it. This is very bad user experience, as an application shouldn't disable the bluetooth without asking the users first. But it will solve your problem, because all bluetooth file descriptors will be disposed automatically. So if the only user is you, it's a possible solution.
If you do plan on publishing this, you should find a real solution... It might not be easy, but this is indeed a major problem and it also seems that many devices are affected by it, so you can't publish your app as long as this problem persists.
This sounds weird, I know.
I am developing an Android app using NFC, the app reads a tag from a samrtcard using IsoDep APDU commands, everything usually works but sometimes it fails, and keep on failing in the followed executions, when it fails it occurs when connecting the tag, the tag has its "mConnectedValue = -1", which means no technology connected but the followed code is executed
card = IsoDep.get(tag);
card.setTimeout(20000);
card.connect();
connect() throws ans excepcion, the exception is catch by this code:
} catch (IOException e) {
...
}
If "e" is inspected with eclipse shows e=null, but the code inside the catch is executed, anyone can explain why e=null? The code above is inside a loop, and followed executions keep on returning that "exception" e=null.
Thank you very much!
Fixed, well, actually the "e=null" still the same, sometimes it throws "IOException e = null" and sometimes just "e= null", in both cases the error is caught by the same code, it might be some kind of bug.
I also solved the connection problem, it was device's fault, the chip was not powerful enough to supply energy to the smartcard, for reading and writing was ok, but when the card make some operations it isn't enough, just moving the phone a little bit upwards solved the problem.
Thanks!