0

I'm using the worldweatheronline api to get weather information for an app I'm building. When i debug the code below, Inputstream "is" always returns null. The code that they gave as an example also has a bunch of log.d that never come up and I believe its because "is" returns as null.

static InputStream getInputStream(String url) {
    InputStream is = null;

    try {

            is = (new URL(url)).openConnection().getInputStream();

            //is = (new URL(url)).openConnection().getInputStream();

    }
    catch (Exception e) 
    {
            System.out.println(is);

    }

    return is;
}

Can anyone help with this? I looked around and other examples are with httpclient and other stuff and I'm not sure if I need to go that route or if the example given was totally incorrect

    03-20 22:12:39.828: W/System.err(4848): android.os.NetworkOnMainThreadException
    03-20 22:12:39.828: W/System.err(4848):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
    03-20 22:12:39.828: W/System.err(4848):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    03-20 22:12:39.828: W/System.err(4848):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    03-20 22:12:39.828: W/System.err(4848):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpConnection.<init> (HttpConnection.java:50)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    03-20 22:12:39.828: W/System.err(4848):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
    03-20 22:12:39.828: W/System.err(4848):     at   libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    03-20 22:12:39.828: W/System.err(4848):     at com.example.seniorproject.WwoApi.getInputStream(WwoApi.java:114)
    03-20 22:12:39.828: W/System.err(4848):     at com.example.seniorproject.LocalWeather.callAPI(LocalWeather.java:23)
    03-20 22:12:39.828: W/System.err(4848):     at com.example.seniorproject.MarkerLocaltionsAdapter.getView(MarkerLocaltionsAdapter.java:95)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.AbsListView.obtainView(AbsListView.java:2627)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1259)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.ListView.onMeasure(ListView.java:1171)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.View.measure(View.java:16831)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
    03-20 22:12:39.828: W/System.err(4848):     at  android.view.View.measure(View.java:16831)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.View.measure(View.java:16831)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    03-20 22:12:39.828: W/System.err(4848):     at  com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.View.measure(View.java:16831)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)
    03-20 22:12:39.828: W/System.err(4848):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    03-20 22:12:39.828: W/System.err(4848):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2586)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.View.measure(View.java:16831)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2187)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1350)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1533)
    03-20 22:12:39.828: W/System.err(4848):     at  android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1247)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6355)
    03-20 22:12:39.828: W/System.err(4848):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
    03-20 22:12:39.828: W/System.err(4848):     at     android.view.Choreographer.doCallbacks(Choreographer.java:591)
    03-20 22:12:39.828: W/System.err(4848):     at  android.view.Choreographer.doFrame(Choreographer.java:561)
    03-20 22:12:39.828: W/System.err(4848):     at    android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
    03-20 22:12:39.828: W/System.err(4848):     at  android.os.Handler.handleCallback(Handler.java:730)
    03-20 22:12:39.828: W/System.err(4848):     at  android.os.Handler.dispatchMessage(Handler.java:92)
    03-20 22:12:39.838: W/System.err(4848):     at android.os.Looper.loop(Looper.java:137)
    03-20 22:12:39.838: W/System.err(4848):     at   android.app.ActivityThread.main(ActivityThread.java:5419)
   03-20 22:12:39.838: W/System.err(4848):  at  java.lang.reflect.Method.invokeNative(Native Method)
    03-20 22:12:39.838: W/System.err(4848):     at   java.lang.reflect.Method.invoke(Method.java:525)
    03-20 22:12:39.838: W/System.err(4848):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
   03-20 22:12:39.838: W/System.err(4848):  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
   03-20 22:12:39.838: W/System.err(4848):  at dalvik.system.NativeStart.main(Native                        Method)

Here is the stack trace, hope this helps.

OK so I looked up the NetworkOnMainThread exception and some suggestions are using strictmode and other say to use asynctask..which is better?

Bnaffy
  • 129
  • 3
  • 14
  • It would be more helpful if you actually printed the stack trace of the exception (or just threw it out of the method, assuming something at a higher level dumped the stack trace) rather than printing `is`, which will *always* be `null` in the exception handler there. – Jason C Mar 21 '14 at 02:50
  • 1
    You've swallowed an exception and returned null, and now you're wondering why you returned null. It's because you swallowed the exception. It would be more to the point not to catch the exception here at all, and let the caller deal with it, rather than ignoring it here and requiring the caller to check for a null which has happened for an unknown reason. Don't write code like this. – user207421 Mar 21 '14 at 04:02

1 Answers1

1

There are many possible reasons, one of which could be that the request is occurring on the "GUI" thread. But as Jason C's comment mentioned dumping the stack trace from the exception will provide more info.

Mirsaes
  • 43
  • 2
  • OK so my mobile typing is slow, but your stack trace confirms my suspicion... NetworkOnMainThreadException – Mirsaes Mar 21 '14 at 03:29