I have made an application that send and receive data via socket it works in Android os < 7.0.0 but when i launche this application in android nougat(7.0.0) it display an error when receiving replay it display android.os.NetworkOnMainThreadException error at lign (dataOutputStream.writeUTF(msgReply);) so how can i fix this error thanks for yours attention.
Code:
String msgReply = "&sim1$extr€"+getIpAddressonly()+"?8080.wifi/";
try
{
dataOutputStream.writeUTF(msgReply); // error at this line
}
catch (IOException e) {
e.printStackTrace();
}
You need to get your network I/O off the main (UI) thread. Start a background thread to do network I/O.
Related
I'm trying to implement an uncaught exception handler in an Android only app built with Xamarin for logging purposes.
AppDomain.CurrentDomain.UnhandledException += (o, e) => exLogger.UncaughtException((Exception)e.ExceptionObject);
TaskScheduler.UnobservedTaskException += (o, e) => exLogger.UncaughtException(e.Exception);
and
public void UncaughtException(Exception ex)
{
try
{
Log.Error(TAG, "Exception caught: {0}", ex.Message ?? "Unknown Exception");
var di = Directory.CreateDirectory(Path.Combine(context.FilesDir.AbsolutePath, TAG));
//Rest of Handler - saves error to file system, attempts to upload to a server
...
}
catch (Exception)
{
Log.Error(TAG, "Exception handler failed");
}
}
The handler in question works with zero problems when I implement it in a very simple "Hello World" application, where I trigger an exception on button-press:
throw new Exception("this is a test exception");
(or similar).
However, When I do the same thing in the application I am actually developing, the app crashes inside the handler, printing no further information as to why this happens.
When I step through my code, the debugger hits a breakpoint at the entry to the handler - e.g. within the try block, but before the Log.Error line. However, attempting to step further than this point results in immediate app crash, without either the Log.Debug line in the try OR catch block being executed - nothing is printed to the ADB Logger. Additionally, the error printed to the ADB Log by W/system.err is only the error I intentionally caused (to trigger the handler) - no information on the crash WITHIN the handler is provided.
Does anyone have any idea what could be causing this crash, or even advice on how to get more information from the ADB logs on what the crash within the handler was caused by? My only train of thought is that, because my app is a WebView application, potentially the WebView itself is consuming the Exception in some manner? Any help is greatly appreciated!
I'm developing my first robust App in Android Studio is an application that communicates with a remote database, for this I am using the AsyncTask class, also made the request in a separate thread, all this I have already done, the question is that I am using a DrawerLayout and every time a menu item is selected, this gets stuck while the data request to the server is done, as I do to make such request is made the one you see DrawerLayout be hidden.
This is how I am running the AsyncTask class.
BD = new TaskExecuteHttpHandler(service, parametros, CONTEXTO);
String resultado="";
try {
resultado = BD.execute().get();
} catch (ExecutionException e) {
e.printStackTrace();
}
I appreciate the help.
I am using the Amazon AWS SDK to download images from S3. Occasionally, when an image is not found an exception "AmazonS3Exception: Status Code: 404" is thrown. However, this seems like an exception which should not crash the app. How can I handle this exception so that it does not crash the app? Apologies, Im a noob to Java & Android.
To follow up on type-a1pha's answer:
If you want to handle an exception gracefully, you would use a try-catch statement. It works something like this:
try {
// Here you put the code that may throw an exception
} catch (AmazonS3Exception e) {
// Looks like we errored out, log the exception and
// tell the user that we 404'd
Log.e(TAG, "Error fetching file from Amazon S3", e);
Toast.makeText(context, "Error 404 while fetching file: file not found", Toast.LENGTH_SHORT).show();
// Insert any other code you need here to recover from the error
} finally {
// Note that the finally part is optional but useful if you want
// to do something after the try-catch statement is finished
// for example, if you were using an inputStream:
inputStream.close();
}
try{
//code throwing exception
} catch (AmazonS3Exception) {}
I wrote simple application, for my Galaxy SII, for permanent connection with remote server. Ones for 5 second it sends and receives data. While application is working nobody can't call to me. Network answers him - interlocutor is unavailable.
The same happend me when I use mail client like K-9. It doesn't matter I use GPRS or 3G connection.
What is a main rule (if is it?) to construct internet application to avoid this problem (I mean problem with ordinary incomming phone connection)?
My ordinary code for sending data (in remote service) is like this:
While (condition)
{
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out.write(data + "\n");
out.flush();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Regards,
Artik
First, if you want to wait for 5 seconds you should use Thread.sleep(5000); not Thread.sleep(500); which is half a second.
Second, consider sending your data to the server using Timer instead of Thread.sleep() and while(condition)
After modifying your code with the above suggestions, try to test from the emulator and simulate a phone call.
In my app i connect to a server, which responds with an xml. I parse it with the SAX Parser, and get the data.
The question is:
What is the best way to handle connection issues?
(At this moment if there is no internet connection available the app simply continues showing the ProgressDialog i implemented)
So you basically do (Pseudo code)
ProgessDialog pd = new ProgressDialog(this).show();
Sax.parseStuff();
pd.dismiss();
In this case, wrap the parsing stuff and cancel the dialog on Exception
ProgessDialog pd = new ProgressDialog(this).show();
try {
Sax.parseStuff();
}
finally {
pd.dismiss(); // or cancel
}
You can also do a try { .. } catch (XYZException e ; pd.cancel(); throw e) if you want to process the Exception in a different layer of your app.
As well as following the suggestion of Heiko Rupp, you can also check for the availability of a network connection prior to performing your download. See my post on the subject.