Reputation: 5032
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;
}
The program never prints out "3" and theres so many warnings in logcat that it's pretty hard for me to decipher what's going on as I haven't used java in quite some time. Any ideas? I was following this code sample...
http://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html
and was trying to modify it to parse xml but I haven't even gotten to test that part yet.
Here is all the warnings logcat gave me right after printing "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)
Upvotes: 0
Views: 855
Reputation: 306
The exception stack trace indicates that you are running this on the main thread. Is that the case? The main thread for Android development shouldn't have network or disk access. You should move you network operations to a thread, then the strict mode guard wont cause the exception.
Upvotes: 2
Reputation: 7605
try this way
InputStream is=callWebservice("http://www.nasa.gov/rss/image_of_the_day.rss");
BufferedReader in=new BufferedReader(new InputStreamReader(
is, "iso-8859-1"));
where callWebservice is as shown below
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;
}
Upvotes: 0
Reputation: 6153
There is a NetworkOnMainThreadException
. Read this link for more information. And also this article.
Upvotes: 2