This question already has answers here:
NetworkOnMainThreadException [duplicate]
(5 answers)
Closed 6 years ago.
My goal is to convert an array of floats to a string array and send it to a remote server in my house via a POST request. I am not using sockets.
URL url = new URL("http:// (destination of the remote server) : (port)");
URLConnection connection = url.openConnection();
This is how I am writing my getInputStream() and getOutputStream method and every time I run my code my activity crashes.
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
Everything works but my activity comes to getOutputStream() and it crashes. I tried commenting out the getOutputStream() part then it came to connection.getInputStream() and crashed.
What could be the reason why this isn't working?
StackTrace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hfad.viscosity/com.hfad.viscosity.ProcessData}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:436)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
at com.hfad.viscosity.ProcessData.onCreate(ProcessData.java:110)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
It appears that you are attempting network activities on the main UI thread for the Activity instead of a background thread. It is required for any activity that involves blocking I/O or heavy processing including network activity to happen on a background thread. You would hence want to use the AsyncTask functionality to achieve this use case.
Refer to this SO question for an example
The second thing is that to perform Internet access from your app you need to provide Internet access permissions to your app. Refer to this other SO question that should help you on how to go about providing this permission.
If you are facing any problems even after performing the above steps, then it is recommended that you post your entire source code (if possible or an equivalent example) so that more folks can help you out.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed last year.
I am developing an android application which requires me the user to sign up and then set profile picture and user name. the registration is working properly and sending the data to Firebase but when it comes to uploading the profile photo, the application reading the gallery and picks the image but does not send the image to the Firebase neither does it set it in the image view. below is the error i get upon examination since the application terminates at that point.
E**/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chatme, PID: 23039
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=123, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/0/1/content://media/external/images/media/211575/ORIGINAL/NONE/image/jpeg/79264842 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F211575/ORIGINAL/NONE/image%2Fjpeg/79264842} }} to activity {com.example.chatme/com.example.chatme.profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4309)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4353)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1678)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageURI(android.net.Uri)' on a null object reference
at com.example.chatme.profile.onActivityResult(profile.java:108)
at android.app.Activity.dispatchActivityResult(Activity.java:7311)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4305)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4353)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1678)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6548)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)**
The error is occurring because the image view in which you setting the image, is null. Did you initialise it? If it is not initialised, it will crash because java will take it as null.setImageUri() but you cannot do anything on null. So initialise the image view in order to get safe from the error
I am trying to open an activity from the Image Cropper library to take images from fragments. When I click on the button to start that activity, the app is crashing and giving that error.
PID: 30654
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1714072 bytes
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:160)
Complete Error:
Caused by: android.os.TransactionTooLargeException: data parcel size 1713760 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1129)
at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4027)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:144)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
There's a limit on the size of a bundle and the data passed into an Intent. Last I checked (it may have changed) that limit was around 1MB. If you need to pass more data than that, you need to send it in some other manner, such as writing it to a file and sending the filename to the activity instead.
I came to this issue when I was trying to parcel around 4000+ objects while firing another activity with intent. So I strongly feel there's some limit to sending the data via intents.
So the main issue is not with intents it is how you parcel your data before/or at the time of transmitting data.
Our application receives the client's request through AIDL, then executes a processing flow, and finally returns the processing result to the client. When I performed a stress test on the application, I found that the following exception would be thrown every time the complete process was executed about 400 times.
The crash log is:
11-05-06-20-59.583 E CrashHandler.java[101](handleExceptions)[1]Exception: java.lang.RuntimeException: Adding window failed
at android.view.ViewRootImpl.setView(ViewRootImpl.java:652)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3518)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2733)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:623)
at android.view.IWindowSession$Stub$Proxy.addToDisplay(IWindowSession.java:828)
at android.view.ViewRootImpl.setView(ViewRootImpl.java:640)
... 12 more
Through the analysis of the memory, it was found that the Handler object was repeatedly created when the AIDL command was initiated, and the Handler object was not cleaned up in time, resulting in a memory leak.
I got this error in my RN application in Android:
06-06 16:37:54.455 7506-7569/system_process E/AudioTrack: Could not
get audio output for session 729, stream type -1, usage 13, sample
rate 48000, format 0x1, channel mask 0x3, flags 0x4 06-06 16:37:54.455
7506-7569/system_process E/SoundPool: Error creating AudioTrack 06-06
16:37:54.924 1286-1286/? E/EGL_emulation: tid 1286:
eglCreateSyncKHR(1669): error 0x3004 (EGL_BAD_ATTRIBUTE) 06-06
16:37:55.190 11821-11894/com.efiat_rn E/unknown:ReactNative: Exception
in native call
java.lang.ClassCastException: com.facebook.react.bridge.ReadableNativeMap cannot be cast to
java.lang.String
at com.facebook.react.bridge.ReadableNativeMap.getString(ReadableNativeMap.java:168)
at com.facebook.react.modules.dialog.DialogModule.showAlert(DialogModule.java:232)
at java.lang.reflect.Method.invoke(Native Method)
at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160)
at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
at android.os.Looper.loop(Looper.java:154)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)
at java.lang.Thread.run(Thread.java:761) 06-06 16:37:55.191 11821-11894/com.efiat_rn E/unknown:ReactNative: Exception in native
call
java.lang.ClassCastException: com.facebook.react.bridge.ReadableNativeMap cannot be cast to
java.lang.String
at com.facebook.react.bridge.ReadableNativeMap.getString(ReadableNativeMap.java:168)
at com.facebook.react.modules.dialog.DialogModule.showAlert(DialogModule.java:232)
at java.lang.reflect.Method.invoke(Native Method)
at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:160)
at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:29)
at android.os.Looper.loop(Looper.java:154)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:192)
at java.lang.Thread.run(Thread.java:761)
It runs perfectly fine on iOS version. It don't show me which JS I do wrong. and I have no clue or ideas on how to fix it. It prompts when I launch the application, when I dismiss the error message, I can see a half-load application, but not clickable. Is there any ideas on how can I start the debugging? Thanks.
In ReactNative you are calling a method of the native bridge which requires String parameters. But what you are actually sending is JsonObject.
Example,
const data = {message: "hello"}; // Consider this as your data
if you send this to the bridge, it will create a NativeArray and send it to native layer (But your layer's method expects String).
Now you have to convert the object to String and send it to the native layer
const strData = JSON.stringify(data);
Now send the strData to the native layer
(Comment below if you need more details or you can tell your error
more clearly to get some clear answer)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
In my application I use a socket to communication with another device. All the time in I have a SocketTimeoutException . This communication I did in service
This is a logs :
11-09 08:44:05.029 5458-6647/pl.teminalmobile W/System.err: java.net.SocketTimeoutException: Read timed out
11-09 08:44:05.030 5458-6647/pl.teminalmobile W/System.err: at java.net.SocketInputStream.socketRead0(Native Method)
11-09 08:44:05.031 5458-6647/pl.teminalmobile W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:151)
11-09 08:44:05.031 5458-6647/pl.teminalmobile W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:120)
11-09 08:44:05.031 5458-6647/pl.teminalmobile W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:106)
11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err: at pl.teminalmobile.Service.Service22.start1(Service22.java:256)
11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err: at pl.teminalmobile.Service.Service22.access$000(Service22.java:75)
11-09 08:44:05.032 5458-6647/pl.teminalmobile W/System.err: at pl.teminalmobile.Service.Service22$19.run(Service22.java:963)
11-09 08:44:05.033 5458-6647/pl.teminalmobile W/System.err: at java.util.TimerThread.mainLoop(Timer.java:555)
11-09 08:44:05.033 5458-6647/pl.teminalmobile W/System.err: at java.util.TimerThread.run(Timer.java:505)
this line couse a SocketTimeOutException :
while ((bytesRead = inputStream.read(content)) != -1) {
Your socket is timing out means that it takes too long to get respond from other device and your request expires before getting response.To tackle this you need to give manual socket timeout like search for how to provide socket timeout of the library you are using. If using socket.io then you need to add
socket.timeout(12000);
Here 12000 are milli seconds or if you are using OkHttp Client then you can add:
clientBuilder.connectTimeout(60, TimeUnit.SECONDS);
Or whichever socket library you are using there is method to give timeout manually so just type your socket name with which you created socket and place '.' after that you have suggestions for available methods in that search for timeout and use that method.
Here is the link use that library demo is also there:
https://github.com/socketio/socket.io-client-java
I had same problem but I fix it by using these approaches
Set Character Encoding to UTF-8 and charset
Although I'm not entirely sure, I don't think you're using Socket.setSoTimeout() correctly.
You're only supposed to use it once for the Socket requestSocket. Try having it at the beginning of your code before you work with the contents of the Socket, or make your code modular by passing it to a function instead of calling the function thrice. That makes it heaps easier to work with Socket.setSoTimeout() from my personal experience.
I show you code you have pasted.
I found you set time 3000ms on line no 117 and 125 for timeout you need to change it maximum up to 15 second
requestSocket.connect(new InetSocketAddress(a, 6666), 3000);
Hope its help
Happy Coding.!