public URL setUrl() throws Exception{
System.out.println("1");
URL iotd=new URL("http://www.nasa.gov/rss/image_of_the_day.rss");
System.out.println("2");
BufferedReader in=new BufferedReader(new InputStreamReader(iotd.openStream()));
System.out.println("3");**//this never gets printed**
//testing xml parser
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
System.out.println("4");
XmlPullParser xpp=factory.newPullParser();
System.out.println("5");
xpp.setInput(in);
System.out.println("6");
int eventType=xpp.getEventType();
System.out.println(eventType+"!!!!!!!!!!!!!!!!");
while(eventType!=XmlPullParser.END_DOCUMENT){
if(eventType==XmlPullParser.START_DOCUMENT){
System.out.println("start");
}
}
in.close();
return iotd;
}
该程序永远不会打印出“3”,并且在logcat中有如此多的警告,因为我很难解读正在发生的事情,因为我在很长一段时间内没有使用过java。有任何想法吗?我正在关注此代码示例...
http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
并试图修改它以解析xml但我还没有测试那部分。
以下是logcat在打印“2”后立即给出的所有警告
07-10 05:31:30.610: W/System.err(534): android.os.NetworkOnMainThreadException
07-10 05:31:30.621: W/System.err(534): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
07-10 05:31:30.621: W/System.err(534): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
07-10 05:31:30.621: W/System.err(534): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
07-10 05:31:30.621: W/System.err(534): at java.net.InetAddress.getAllByName(InetAddress.java:220)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
07-10 05:31:30.621: W/System.err(534): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
07-10 05:31:30.630: W/System.err(534): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-10 05:31:30.630: W/System.err(534): at java.net.URL.openStream(URL.java:462)
07-10 05:31:30.630: W/System.err(534): at com.wajumbie.nasadailyimage.RssParse.setUrl(RssParse.java:14)
07-10 05:31:30.630: W/System.err(534): at com.wajumbie.nasadailyimage.NasaDailyImage.onCreate(NasaDailyImage.java:29)
07-10 05:31:30.630: W/System.err(534): at android.app.Activity.performCreate(Activity.java:4465)
07-10 05:31:30.630: W/System.err(534): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-10 05:31:30.630: W/System.err(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-10 05:31:30.630: W/System.err(534): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-10 05:31:30.640: W/System.err(534): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-10 05:31:30.640: W/System.err(534): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-10 05:31:30.640: W/System.err(534): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 05:31:30.640: W/System.err(534): at android.os.Looper.loop(Looper.java:137)
07-10 05:31:30.640: W/System.err(534): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 05:31:30.640: W/System.err(534): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 05:31:30.640: W/System.err(534): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 05:31:30.640: W/System.err(534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 05:31:30.640: W/System.err(534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 05:31:30.640: W/System.err(534): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
有一个NetworkOnMainThreadException
。有关详细信息,请阅读this link。还有this article。
答案 1 :(得分:2)
异常堆栈跟踪表明您在主线程上运行它。 是这样的吗? Android开发的主要线程不应具有网络或磁盘访问权限。 您应该将网络操作移动到一个线程,然后严格模式防护不会导致异常。
答案 2 :(得分:0)
试试这种方式
InputStream is=callWebservice("http://www.nasa.gov/rss/image_of_the_day.rss");
BufferedReader in=new BufferedReader(new InputStreamReader(
is, "iso-8859-1"));
其中 callWebservice 如下所示
private InputStream callWebservice(String serviceURL) {
HttpClient client=new DefaultHttpClient();
HttpGet getRequest=new HttpGet();
try {
// construct a URI object
getRequest.setURI(new URI(serviceURL));
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
// buffer reader to read the response
BufferedReader in=null;
// the service response
HttpResponse response=null;
try {
// execute the request
response = client.execute(getRequest);
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException", e.toString());
} catch (IOException e) {
Log.e("IO exception", e.toString());
}
if(response!=null)
try {
return response.getEntity().getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
else
return null;
return null;
}