Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

making a network connection

I have an Android app.  I cannot make a network connection.  I always get an exception.   I tried using both the java net library as well as Apache.  I've tried two different ways show below.  The exception is below too.   Anyone have an idea?

url = new URL("http://photojournal.jpl.nasa.gov/catalog/PIA16442");
                        Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

OR:

URL url = new URL("http://photojournal.jpl.nasa.gov/rss/new");
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();


EXCEPTION:

11-12 00:09:03.394: E/AndroidRuntime(3465): FATAL EXCEPTION: main
11-12 00:09:03.394: E/AndroidRuntime(3465): android.os.NetworkOnMainThreadException
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at com.example.assignment_four.MainActivity$IconicAdapter.getView(MainActivity.java:103)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.AbsListView.obtainView(AbsListView.java:2267)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.ListView.onMeasure(ListView.java:1156)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.View.measure(View.java:15172)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.View.measure(View.java:15172)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.View.measure(View.java:15172)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.View.measure(View.java:15172)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.View.measure(View.java:15172)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.Choreographer.doFrame(Choreographer.java:525)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.os.Handler.handleCallback(Handler.java:615)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.os.Looper.loop(Looper.java:137)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at java.lang.reflect.Method.invokeNative(Native Method)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at java.lang.reflect.Method.invoke(Method.java:511)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 00:09:03.394: E/AndroidRuntime(3465):       at dalvik.system.NativeStart.main(Native Method)
11-12 00:09:13.505: I/Process(3465): Sending signal. PID: 3465 SIG: 9
Avatar of HLRosenberger
HLRosenberger
Flag of United States of America image

ASKER

Oh, and if I do this in a Java desktop app, all works OK.
Avatar of girionis
Have you given your application the INTERNET access permission in your manifest file?

http://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application
I added that permission.   See attached   I still get the error.
I added that permission.   See attached   I still get the error.
permission.png
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  Yes, if I set the policy to LAX, it works OK.  I'm new to Android, and I did not realize I should be doing the network access in another task and not the main UI  activity.