0

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) 
Hamza Khan
  • 131
  • 10

1 Answers1

1

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.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • That one does it for HttpClient which is deprecated now. Do you have any idea how it can be done using HttpURLConnection? – Hamza Khan Aug 14 '16 at 10:45