I have problem with Android using the class HttpUrlConnection my method is this:
public InputStream test() {
URL url = null;
try {
url = new URL("http://www.android.com/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in= null;
try {
in = new BufferedInputStream(urlConnection.getInputStream());
}catch(IOException e){
e.printStackTrace();
}
finally {
urlConnection.disconnect();
}
return in;
}
When i execute this method android generates this exception:
05-03 15:15:19.116: W/System.err(1085): android.os.NetworkOnMainThreadException
05-03 15:15:19.116: W/System.err(1085): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-03 15:15:19.116: W/System.err(1085): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-03 15:15:19.116: W/System.err(1085): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-03 15:15:19.116: W/System.err(1085): at java.net.InetAddress.getAllByName(InetAddress.java:220)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection.(HttpConnection.java:71)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
05-03 15:15:19.126: W/System.err(1085): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
05-03 15:15:19.136: W/System.err(1085): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
05-03 15:15:19.136: W/System.err(1085): at com.BertacchiMazzoni.HTTPClient.HttpClientTutorial.test(HttpClientTutorial.java:35)
05-03 15:15:19.136: W/System.err(1085): at com.BertacchiMazzoni.HTTPClient.HTTPClientActivity.onCreate(HTTPClientActivity.java:18)
05-03 15:15:19.136: W/System.err(1085): at android.app.Activity.performCreate(Activity.java:4465)
05-03 15:15:19.136: W/System.err(1085): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 15:15:19.136: W/System.err(1085): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 15:15:19.146: W/System.err(1085): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 15:15:19.146: W/System.err(1085): at android.os.Looper.loop(Looper.java:137)
05-03 15:15:19.146: W/System.err(1085): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-03 15:15:19.146: W/System.err(1085): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:15:19.146: W/System.err(1085): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 15:15:19.146: W/System.err(1085): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-03 15:15:19.146: W/System.err(1085): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-03 15:15:19.146: W/System.err(1085): at dalvik.system.NativeStart.main(Native Method)
Can i resolve this problem? Where is the error?
Thanks a lot!
Regards!
A quick google for the exception leads you here: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
Which states:
So basically I’m guessing you’re performing your network operation on the main application thread; and for Honeycomb and up it looks like you can’t do this. It’s a bad idea anyway, as it’s likely to make your application hang and appear unresponsive. You’d need to use another thread or handler or asynctask or Loader to perform the operation.