android - downloadUrl returns null Stream -
i'm trying parse rss
feed. it's returning null value. debugged & checked. in onpostexecute()
- value of result null. here tried code:
here rssfragment.java
package com.example.samplerssfeed; import java.io.ioexception; import java.io.inputstream; import java.net.httpurlconnection; import java.net.url; import java.util.arraylist; import java.util.list; import org.xmlpull.v1.xmlpullparserexception; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listview; import android.widget.toast; public class rssfragment extends fragment { private listview mlistview; private static final string url = "https://news.google.com/?output=rss"; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view=inflater.inflate(r.layout.fragment_rs, container, false); mlistview=(listview)view.findviewbyid(r.id.listview); downloadxmltask xmltask=new downloadxmltask(); xmltask.execute(url); return view; } private class downloadxmltask extends asynctask<string, void, list<item>>{ @override protected list<item> doinbackground(string... urls) { list<item> items = new arraylist<item>(); try { items=loadxmlfromnetwork(urls[0]); } catch (xmlpullparserexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return items; } @override protected void onpostexecute(list<item> result) { if(result==null){ toast.maketext(getactivity(), "parsing faied", toast.length_long).show(); } else{ rssadapter adapter=new rssadapter(getactivity(),result); log.d("parsing", "calling adapter"); mlistview.setadapter(adapter); } } } private list<item> loadxmlfromnetwork(string urlstring) throws xmlpullparserexception, ioexception{ inputstream stream=null; rssparser rssparser=new rssparser(); list<item> items=null; try { stream = downloadurl(urlstring); items = rssparser.parse(stream); // makes sure inputstream closed after app // finished using it. } { if (stream != null) { stream.close(); } } return items; } // given string representation of url, sets connection , gets // input stream. private inputstream downloadurl(string urlstring) throws ioexception { url url = new url(urlstring); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setreadtimeout(10000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); // starts query conn.connect(); return conn.getinputstream(); } }
this fragment class, in used async
task. method items=loadxmlfromnetwork(urls[0])
returning null value
here rssparser.java
package com.example.samplerssfeed; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.list; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import android.util.xml; public class rssparser { private static final string ns=null; //instantiating pull parser public list<item> parse(inputstream in) throws xmlpullparserexception,ioexception{ try{ xmlpullparser parser=xml.newpullparser(); parser.setfeature(xmlpullparser.feature_process_namespaces, false); parser.setinput(in,null); parser.nexttag(); return reedchannel(parser); } finally{ in.close(); } } //reading feed private list<item> reedchannel(xmlpullparser parser) throws xmlpullparserexception, ioexception{ list<item> items = new arraylist<item>(); parser.require(xmlpullparser.start_tag, ns, "channel"); while (parser.next() != xmlpullparser.end_tag) { if (parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); // starts looking item tag if (name.equals("item")) { items.add(readitem(parser)); } else { skip(parser); } } return items; } private item readitem(xmlpullparser parser) throws xmlpullparserexception, ioexception{ parser.require(xmlpullparser.start_tag, ns, "item"); string title=null; string link=null; string summary=null; while (parser.next() != xmlpullparser.end_tag) { if (parser.geteventtype() != xmlpullparser.start_tag) { continue; } string name = parser.getname(); if (name.equals("title")) { title = readtitle(parser); } else if (name.equals("description")) { summary = readdescription(parser); } else if (name.equals("link")) { link = readlink(parser); } else { skip(parser); } } return new item(title, summary, link); } // processes title tags in feed. private string readtitle(xmlpullparser parser) throws ioexception, xmlpullparserexception { parser.require(xmlpullparser.start_tag, ns, "title"); string title = readtext(parser); parser.require(xmlpullparser.end_tag, ns, "title"); return title; } // processes link tags in feed. private string readlink(xmlpullparser parser) throws ioexception, xmlpullparserexception { string link = ""; parser.require(xmlpullparser.start_tag, ns, "link"); string tag = parser.getname(); string reltype = parser.getattributevalue(null, "rel"); if (tag.equals("link")) { if (reltype.equals("alternate")){ link = parser.getattributevalue(null, "href"); parser.nexttag(); } } parser.require(xmlpullparser.end_tag, ns, "link"); return link; } // processes summary tags in feed. private string readdescription(xmlpullparser parser) throws ioexception, xmlpullparserexception { parser.require(xmlpullparser.start_tag, ns, "description"); string summary = readtext(parser); parser.require(xmlpullparser.end_tag, ns, "description"); return summary; } // tags title , summary, extracts text values. private string readtext(xmlpullparser parser) throws ioexception, xmlpullparserexception { string result = ""; if (parser.next() == xmlpullparser.text) { result = parser.gettext(); parser.nexttag(); } return result; } private void skip(xmlpullparser parser) throws xmlpullparserexception, ioexception { if (parser.geteventtype() != xmlpullparser.start_tag) { throw new illegalstateexception(); } int depth = 1; while (depth != 0) { switch (parser.next()) { case xmlpullparser.end_tag: depth--; break; case xmlpullparser.start_tag: depth++; break; } } } }
here rssadapter.java
package com.example.samplerssfeed; import java.util.list; import android.content.context; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; public class rssadapter extends arrayadapter<item>{ private list<item> mitems; private context mcontext; private layoutinflater minflater; public rssadapter(context context, list<item> items) { super(context, r.layout.rss_items, items); mcontext=context; mitems=items; minflater=(layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); } private static class rssholder{ textview mtextview1,mtextview2; } @override public view getview(int position, view convertview, viewgroup parent) { rssholder holder; if (convertview == null) { holder = new rssholder(); convertview = minflater.inflate(r.layout.rss_items, parent, false); holder.mtextview1 = (textview) convertview .findviewbyid(r.id.textview1); holder.mtextview2 = (textview) convertview .findviewbyid(r.id.textview2); log.d("parsing", "setting listview"); convertview.settag(holder); } else { holder = (rssholder) convertview.gettag(); } string title=mitems.get(position).tostring(); string description=mitems.get(position).tostring(); holder.mtextview1.settext(title); holder.mtextview2.settext(description); log.d("parsing", "return views"); return convertview; } }
here item.java
package com.example.samplerssfeed; public class item { public final string title; public final string link; public final string summary; public item(string title,string link,string summary){ this.title=title; this.link=link; this.summary=summary; } }
can tell me why returning null value
my log cat entries:
06-03 04:57:14.848: d/dalvikvm(385): gc_for_alloc freed 846k, 22% free 6130k/7804k, paused 263ms, total 275ms 06-03 04:57:15.258: d/androidruntime(2959): >>>>>> androidruntime start com.android.internal.os.runtimeinit <<<<<< 06-03 04:57:15.268: d/androidruntime(2959): checkjni on 06-03 04:57:15.638: d/dalvikvm(2959): trying load lib libjavacore.so 0x0 06-03 04:57:15.678: d/dalvikvm(2959): added shared lib libjavacore.so 0x0 06-03 04:57:15.748: d/dalvikvm(2959): trying load lib libnativehelper.so 0x0 06-03 04:57:15.758: d/dalvikvm(2959): added shared lib libnativehelper.so 0x0 06-03 04:57:15.758: d/dalvikvm(2959): no jni_onload found in libnativehelper.so 0x0, skipping init 06-03 04:57:15.928: d/lightsservice(385): excessive delay setting light: 1003ms 06-03 04:57:16.438: w/recognitionmanagerservice(385): no available voice recognition services found user 0 06-03 04:57:16.778: d/dalvikvm(2959): note: class landroid/app/activitymanagernative; has 179 unimplemented (abstract) methods 06-03 04:57:16.848: d/dalvikvm(544): gc_for_alloc freed 577k, 37% free 4677k/7324k, paused 146ms, total 157ms 06-03 04:57:16.968: i/choreographer(544): skipped 304 frames! application may doing work on main thread. 06-03 04:57:27.448: d/androidruntime(2968): >>>>>> androidruntime start com.android.internal.os.runtimeinit <<<<<< 06-03 04:57:27.478: d/androidruntime(2968): checkjni on 06-03 04:57:27.678: d/dalvikvm(2968): trying load lib libjavacore.so 0x0 06-03 04:57:27.688: d/dalvikvm(2968): added shared lib libjavacore.so 0x0 06-03 04:57:27.748: d/dalvikvm(2968): trying load lib libnativehelper.so 0x0 06-03 04:57:27.748: d/dalvikvm(2968): added shared lib libnativehelper.so 0x0 06-03 04:57:27.748: d/dalvikvm(2968): no jni_onload found in libnativehelper.so 0x0, skipping init 06-03 04:57:28.338: d/dalvikvm(2968): note: class landroid/app/activitymanagernative; has 179 unimplemented (abstract) methods 06-03 04:57:29.648: e/memtrack(2968): couldn't load memtrack module (no such file or directory) 06-03 04:57:29.648: e/android.os.debug(2968): failed load memtrack module: -2 06-03 04:57:30.278: d/androidruntime(2968): calling main entry com.android.commands.pm.pm 06-03 04:57:30.448: d/androidruntime(2968): shutting down vm 06-03 04:57:30.468: d/dalvikvm(2968): debugger has detached; object registry had 1 entries 06-03 04:57:31.728: d/androidruntime(2979): >>>>>> androidruntime start com.android.internal.os.runtimeinit <<<<<< 06-03 04:57:31.738: d/androidruntime(2979): checkjni on 06-03 04:57:31.928: d/dalvikvm(2979): trying load lib libjavacore.so 0x0 06-03 04:57:31.938: d/dalvikvm(2979): added shared lib libjavacore.so 0x0 06-03 04:57:32.018: d/dalvikvm(2979): trying load lib libnativehelper.so 0x0 06-03 04:57:32.018: d/dalvikvm(2979): added shared lib libnativehelper.so 0x0 06-03 04:57:32.028: d/dalvikvm(2979): no jni_onload found in libnativehelper.so 0x0, skipping init 06-03 04:57:32.528: d/dalvikvm(2979): note: class landroid/app/activitymanagernative; has 179 unimplemented (abstract) methods 06-03 04:57:33.858: e/memtrack(2979): couldn't load memtrack module (no such file or directory) 06-03 04:57:33.858: e/android.os.debug(2979): failed load memtrack module: -2 06-03 04:57:34.518: d/androidruntime(2979): calling main entry com.android.commands.am.am 06-03 04:57:34.688: i/activitymanager(385): start u0 {act=android.intent.action.main cat=[android.intent.category.launcher] flg=0x10000000 cmp=com.example.samplerssfeed/.rssactivity} pid 2979 06-03 04:57:34.778: d/gralloc(49): registering buffer in process created it. may cause memory ordering problems. 06-03 04:57:34.778: e/libegl(49): called unimplemented opengl es api 06-03 04:57:34.788: e/libegl(49): called unimplemented opengl es api 06-03 04:57:34.788: e/libegl(49): called unimplemented opengl es api 06-03 04:57:34.788: e/libegl(49): called unimplemented opengl es api 06-03 04:57:34.788: e/surfaceflinger(49): glcheckframebufferstatusoes error 1945814895 06-03 04:57:34.788: e/surfaceflinger(49): got gl_framebuffer_complete_oes error while taking screenshot 06-03 04:57:34.788: e/libegl(49): called unimplemented opengl es api 06-03 04:57:34.788: e/libegl(49): called unimplemented opengl es api 06-03 04:57:34.828: w/windowmanager(385): screenshot failure taking screenshot (164x246) layer 21010 06-03 04:57:34.928: d/androidruntime(2979): shutting down vm 06-03 04:57:34.948: d/jdwp(2979): got wake-up signal, bailing out of select 06-03 04:57:34.948: d/dalvikvm(2979): debugger has detached; object registry had 1 entries 06-03 04:57:35.088: i/activitymanager(385): start proc com.example.samplerssfeed activity com.example.samplerssfeed/.rssactivity: pid=2990 uid=10067 gids={50067, 3003} 06-03 04:57:35.158: d/dalvikvm(2990): not late-enabling checkjni (already on) 06-03 04:57:35.218: i/choreographer(385): skipped 47 frames! application may doing work on main thread. 06-03 04:57:35.538: i/choreographer(385): skipped 43 frames! application may doing work on main thread. 06-03 04:57:35.648: i/dalvikvm(2990): checkjni enabled: not enabling jni app bug workarounds. 06-03 04:57:36.108: i/choreographer(385): skipped 66 frames! application may doing work on main thread. 06-03 04:57:36.228: i/choreographer(385): skipped 30 frames! application may doing work on main thread. 06-03 04:57:36.338: i/choreographer(385): skipped 63 frames! application may doing work on main thread. 06-03 04:57:36.448: i/choreographer(385): skipped 66 frames! application may doing work on main thread. 06-03 04:57:37.498: i/choreographer(2990): skipped 145 frames! application may doing work on main thread. 06-03 04:57:37.548: d/gralloc_goldfish(2990): emulator without gpu emulation detected. 06-03 04:57:37.678: i/activitymanager(385): displayed com.example.samplerssfeed/.rssactivity: +2s714ms 06-03 04:57:37.778: i/choreographer(2990): skipped 51 frames! application may doing work on main thread. 06-03 04:57:37.788: i/choreographer(385): skipped 39 frames! application may doing work on main thread. 06-03 04:57:37.908: i/choreographer(385): skipped 43 frames! application may doing work on main thread. 06-03 04:57:37.998: i/choreographer(385): skipped 40 frames! application may doing work on main thread. 06-03 04:57:38.258: d/dalvikvm(2990): gc_for_alloc freed 83k, 5% free 3128k/3276k, paused 207ms, total 213ms 06-03 04:57:41.768: d/httpexample(2990): response is:200 06-03 04:57:41.838: d/dalvikvm(2990): gc_for_alloc freed 373k, 12% free 3259k/3700k, paused 57ms, total 57ms 06-03 04:57:42.638: d/lightsservice(385): excessive delay setting light: 69ms 06-03 04:57:42.818: d/lightsservice(385): excessive delay setting light: 178ms 06-03 04:57:42.948: d/lightsservice(385): excessive delay setting light: 103ms 06-03 04:57:43.038: d/lightsservice(385): excessive delay setting light: 64ms 06-03 04:57:43.398: d/dalvikvm(2990): gc_for_alloc freed 487k, 15% free 3275k/3832k, paused 45ms, total 46ms 06-03 04:57:43.478: d/parsing(2990): calling adapter 06-03 04:58:00.178: i/choreographer(544): skipped 38 frames! application may doing work on main thread. 06-03 04:58:35.628: d/lightsservice(385): excessive delay setting light: 82ms 06-03 04:58:35.678: d/lightsservice(385): excessive delay setting light: 54ms 06-03 04:58:35.788: d/lightsservice(385): excessive delay setting light: 69ms 06-03 04:58:35.908: d/lightsservice(385): excessive delay setting light: 102ms 06-03 05:00:01.018: d/connectivityservice(385): sampling interval elapsed, updating statistics .. 06-03 05:00:01.068: d/connectivityservice(385): done. 06-03 05:00:01.078: d/connectivityservice(385): setting timer 720seconds 06-03 05:04:27.788: d/dalvikvm(944): gc_for_alloc freed 845k, 15% free 5225k/6140k, paused 66ms, total 67ms 06-03 05:04:36.198: d/dalvikvm(523): gc_for_alloc freed 591k, 17% free 3240k/3900k, paused 48ms, total 50ms
this seems culprit:
items = rssparser.parse(stream);
perhaps, parse
seems return null
.
Comments
Post a Comment