我正在使用worldweatheronline api获取我正在构建的应用的天气信息。当我调试下面的代码时,Inputstream"是"始终返回null。他们作为一个例子提供的代码也有一堆永远不会出现的log.d,我相信它是因为"是#34;返回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;
}
任何人都可以帮忙吗?我环顾四周,其他一些例子是httpclient和其他东西,我不确定我是否需要走这条路线或者给出的例子是完全错误的
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)
这是堆栈跟踪,希望这有帮助。
好的,所以我查找了NetworkOnMainThread异常,一些建议使用strictmode,其他人说使用asynctask ..哪个更好?
答案 0 :(得分:1)
有许多可能的原因,其中一个可能是请求发生在“GUI”线程上。但正如Jason C的评论所提到的那样,从异常中倾倒堆栈跟踪将提供更多信息。