android - AsyncTask with above API 11 makes app crashed -


i using asynctask , gson parse feed , works properly. in 1 fragment getting forced close of app on device above api 11. okay in devices below api 11. code:

public class latestsubmissions extends sherlocklistfragment {      sharedpreferences prefs;     latestsubmissionsadapter adapter = null;     arraylist<hashmap<string, string>> submissions = new arraylist<hashmap<string, string>>();     private getsubmissionslisttask submissiontask = null;      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {          connectiondetector cd = new connectiondetector(getsherlockactivity());         if (cd.isconnectingtointernet()) {             prefs = getsherlockactivity().getsharedpreferences(                     commonutils.preference_name, 0);             submissiontask = new getsubmissionslisttask();             submissiontask.execute(commonutils.user_submission_url                     + prefs.getstring(commonutils.key_user_id, "339") + "/10");         } else {             mainactivity.networkavailabilitynotice(getsherlockactivity());         }         return super.oncreateview(inflater, container, savedinstancestate);     }      protected class getsubmissionslisttask extends             asynctask<string, void, inputstreamreader> {          @override         protected inputstreamreader doinbackground(string... params) {             return new jsondownloader().getjsonstringfromurl(params[0]);         }          @override         protected void onpostexecute(inputstreamreader isr) {             if (isr != null) {                 mainactivity activity = (mainactivity) getsherlockactivity();                 jsonreader reader = new jsonreader(isr);                 try {                     reader.beginobject();                     reader.nextname(); // name                     reader.skipvalue();                     reader.nextname(); // uname                     reader.skipvalue();                     string subs = reader.nextname(); // subs                     if (subs.equals(commonutils.key_submission)) {                         reader.beginarray();                         while (reader.hasnext()) {                             hashmap<string, string> map = new hashmap<string, string>();                             reader.beginarray();                             reader.skipvalue(); // submission id                             string txt = "latest problem";                             problems obj = mainactivity.problems.get(reader                                     .nextint());                             if (obj != null) {                                 txt = obj.getproblemsinfo();                             }                             map.put(commonutils.key_problem_id, txt);                             verdict verdict = activity.verdicts.get(reader                                     .nextstring()); // verdict id                             map.put(commonutils.key_verdict_color,                                     verdict.verdictcolorhex);                             map.put(commonutils.key_verdict_id, verdict.name);                             double exectime = reader.nextdouble() / 1000; // execution                                                                             // time                             map.put(commonutils.key_runtime,                                     exectime.tostring() + "s");                             reader.skipvalue(); // submission time                             map.put(commonutils.key_language_id,                                     activity.languagecode.get(reader                                             .nextstring())); // lanugage                                                                 // id                             map.put(commonutils.key_submission_rank,                                     reader.nextstring()); // rank                             submissions.add(map);                             reader.endarray();                         }                         reader.endarray();                     }                     reader.endobject();                     reader.close();                 } catch (ioexception e) {                     if (commonutils.isdebuggable) {                         log.e("gson parser",                                 "error parsing data " + e.tostring());                     }                 }                 collections.reverse(submissions);                 adapter = new latestsubmissionsadapter(getsherlockactivity(),                         submissions);                 setlistadapter(adapter);             }         }     }      @override     public void onstop() {         super.onstop();         if (submissiontask != null                 && submissiontask.getstatus() != asynctask.status.finished) {             submissiontask.cancel(true);         }     } } 

ihave tested in several devices , emulators different api level , forced close in 2 devices ics , jellybean. log cat:

08-19 01:57:16.961: e/androidruntime(710): fatal exception: main 08-19 01:57:16.961: e/androidruntime(710): android.os.networkonmainthreadexception 08-19 01:57:16.961: e/androidruntime(710):  @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1099) 08-19 01:57:16.961: e/androidruntime(710):  @ libcore.io.blockguardos.recvfrom(blockguardos.java:163) 08-19 01:57:16.961: e/androidruntime(710):  @ libcore.io.iobridge.recvfrom(iobridge.java:503) 08-19 01:57:16.961: e/androidruntime(710):  @ java.net.plainsocketimpl.read(plainsocketimpl.java:488) 08-19 01:57:16.961: e/androidruntime(710):  @ java.net.plainsocketimpl.access$000(plainsocketimpl.java:46) 08-19 01:57:16.961: e/androidruntime(710):  @ java.net.plainsocketimpl$plainsocketinputstream.read(plainsocketimpl.java:240) 08-19 01:57:16.961: e/androidruntime(710):  @ org.apache.http.impl.io.abstractsessioninputbuffer.fillbuffer(abstractsessioninputbuffer.java:103) 08-19 01:57:16.961: e/androidruntime(710):  @ org.apache.http.impl.io.abstractsessioninputbuffer.read(abstractsessioninputbuffer.java:134) 08-19 01:57:16.961: e/androidruntime(710):  @ org.apache.http.impl.io.contentlengthinputstream.read(contentlengthinputstream.java:174) 08-19 01:57:16.961: e/androidruntime(710):  @ org.apache.http.conn.eofsensorinputstream.read(eofsensorinputstream.java:159) 08-19 01:57:16.961: e/androidruntime(710):  @ java.io.inputstreamreader.read(inputstreamreader.java:244) 08-19 01:57:16.961: e/androidruntime(710):  @ com.google.gson.stream.jsonreader.fillbuffer(jsonreader.java:1263) 08-19 01:57:16.961: e/androidruntime(710):  @ com.google.gson.stream.jsonreader.nextquotedvalue(jsonreader.java:1005) 08-19 01:57:16.961: e/androidruntime(710):  @ com.google.gson.stream.jsonreader.nextstring(jsonreader.java:811) 08-19 01:57:16.961: e/androidruntime(710):  @ me.kaidul.uhunt.ranklist$getranklisttask.onpostexecute(ranklist.java:85) 08-19 01:57:16.961: e/androidruntime(710):  @ me.kaidul.uhunt.ranklist$getranklisttask.onpostexecute(ranklist.java:1) 08-19 01:57:16.961: e/androidruntime(710):  @ android.os.asynctask.finish(asynctask.java:602) 08-19 01:57:16.961: e/androidruntime(710):  @ android.os.asynctask.access$600(asynctask.java:156) 08-19 01:57:16.961: e/androidruntime(710):  @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:615) 08-19 01:57:16.961: e/androidruntime(710):  @ android.os.handler.dispatchmessage(handler.java:99) 08-19 01:57:16.961: e/androidruntime(710):  @ android.os.looper.loop(looper.java:137) 08-19 01:57:16.961: e/androidruntime(710):  @ android.app.activitythread.main(activitythread.java:4424) 08-19 01:57:16.961: e/androidruntime(710):  @ java.lang.reflect.method.invokenative(native method) 08-19 01:57:16.961: e/androidruntime(710):  @ java.lang.reflect.method.invoke(method.java:511) 08-19 01:57:16.961: e/androidruntime(710):  @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 08-19 01:57:16.961: e/androidruntime(710):  @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 08-19 01:57:16.961: e/androidruntime(710):  @ dalvik.system.nativestart.main(native method) 

i can't figure out how happening. used same type code in other fragments , worked well. 2 fragments incluing encounter problem.

you need entire reading of input stream in doinbackground. you're doing opening connection in doinbackground , obtaining reader result. however, you're trying read data off network in onpostexecute, executing on main thread.

protected class getsubmissionslisttask extends         asynctask<string, void, arraylist<hashmap<string, string>>> {      @override     protected arraylist<hashmap<string, string>> doinbackground(string... params) {         inputstreamreader isr = new jsondownloader().getjsonstringfromurl(params[0]);         arraylist<hashmap<string, string>> result = null;         if (isr != null) {             result = new arraylist<hashmap<string, string>>();             mainactivity activity = (mainactivity) getsherlockactivity();             jsonreader reader = new jsonreader(isr);             try {                 reader.beginobject();                 reader.nextname(); // name                 reader.skipvalue();                 reader.nextname(); // uname                 reader.skipvalue();                 string subs = reader.nextname(); // subs                 if (subs.equals(commonutils.key_submission)) {                     reader.beginarray();                     while (reader.hasnext()) {                         hashmap<string, string> map = new hashmap<string, string>();                         reader.beginarray();                         reader.skipvalue(); // submission id                         string txt = "latest problem";                         problems obj = mainactivity.problems.get(reader                                 .nextint());                         if (obj != null) {                             txt = obj.getproblemsinfo();                         }                         map.put(commonutils.key_problem_id, txt);                         verdict verdict = activity.verdicts.get(reader                                 .nextstring()); // verdict id                         map.put(commonutils.key_verdict_color,                                 verdict.verdictcolorhex);                         map.put(commonutils.key_verdict_id, verdict.name);                         double exectime = reader.nextdouble() / 1000; // execution                                                                         // time                         map.put(commonutils.key_runtime,                                 exectime.tostring() + "s");                         reader.skipvalue(); // submission time                         map.put(commonutils.key_language_id,                                 activity.languagecode.get(reader                                         .nextstring())); // lanugage                                                             // id                         map.put(commonutils.key_submission_rank,                                 reader.nextstring()); // rank                         result.add(map);                         reader.endarray();                     }                     reader.endarray();                 }                 reader.endobject();             } catch (ioexception e) {                 if (commonutils.isdebuggable) {                     log.e("gson parser",                             "error parsing data " + e.tostring());                 }                 result = null;             } {                 reader.close(); // need close, after exception             }         }         if (result != null) collections.reverse(result);         return result;     }      @override     protected void onpostexecute(arraylist<hashmap<string, string>> result) {         if (result != null) {             submissions = result; // still need this?             adapter = new latestsubmissionsadapter(getsherlockactivity(),                     result);             setlistadapter(adapter);         }     } } 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo