Bharanikumar
Bharanikumar

Reputation: 25733

BufferedReader getting error android.os.NetworkOnMainThreadException

unable to read response from server, getting " android.os.NetworkOnMainThreadException " Error.

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String serverResponse = in.readLine();

 W/System.err(1212):        android.os.NetworkOnMainThreadException                                                
 W/System.err(1212):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)       
 W/System.err(1212):    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)                             
 W/System.err(1212):    at libcore.io.IoBridge.recvfrom(IoBridge.java:506)                                     
 W/System.err(1212):    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)                             
 W/System.err(1212):    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)                        
 W/System.err(1212):    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 

Upvotes: 2

Views: 926

Answers (2)

NARESH REDDY
NARESH REDDY

Reputation: 682

According to android documents all the network handling code should not be placed in main thread. It will work for older versions not for newer versions. So use thread or async task.

Upvotes: 0

Apoorv
Apoorv

Reputation: 13520

Move this code to the doInBackground method of an AsyncTask or Thread as you are getting data from the Internet and any calls made to the Internet need to be done in the background Thread not on the UI Thread or it will give error.

Upvotes: -1

Related Questions