json - android FATAL EXCEPTION: AsyncTask #2 -
i'm getting fatal exception: asynctask #2 in application. can please tell me why , tell me need do. pretty confused right , need little help.
07-25 11:18:27.014: e/androidruntime(784): fatal exception: asynctask #2 07-25 11:18:27.014: e/androidruntime(784): java.lang.runtimeexception: error occured while executing doinbackground() 07-25 11:18:27.014: e/androidruntime(784): @ android.os.asynctask$3.done(asynctask.java:299) 07-25 11:18:27.014: e/androidruntime(784): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 07-25 11:18:27.014: e/androidruntime(784): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 07-25 11:18:27.014: e/androidruntime(784): @ java.util.concurrent.futuretask.run(futuretask.java:239) 07-25 11:18:27.014: e/androidruntime(784): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 07-25 11:18:27.014: e/androidruntime(784): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 07-25 11:18:27.014: e/androidruntime(784): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 07-25 11:18:27.014: e/androidruntime(784): @ java.lang.thread.run(thread.java:856) 07-25 11:18:27.014: e/androidruntime(784): caused by: java.lang.nullpointerexception 07-25 11:18:27.014: e/androidruntime(784): @ com.theproblemsolver.jsonparsser.getjsonfromuri(jsonparsser.java:35) 07-25 11:18:27.014: e/androidruntime(784): @ com.theproblemsolver.asynctask.doinbackground(mainactivity.java:64) 07-25 11:18:27.014: e/androidruntime(784): @ com.theproblemsolver.asynctask.doinbackground(mainactivity.java:1) 07-25 11:18:27.014: e/androidruntime(784): @ android.os.asynctask$2.call(asynctask.java:287) 07-25 11:18:27.014: e/androidruntime(784): @ java.util.concurrent.futuretask.run(futuretask.java:234)
main class
public class mainactivity extends activity { edittext et; button getanswer; jsonarray resultsset = null; {} @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button getanswer = (button) findviewbyid(r.id.button1); getanswer.setonclicklistener(new onclicklistener() { public string uri; @override public void onclick(view v) { new asynctask().execute(uri); intent = new intent(mainactivity.this, listview.class); startactivity(i); } }); } } class asynctask extends asynctask<string, void, jsonobject> { private static final string tag_results = "resultsset"; private static final string tag_subject = "subject"; private static final string tag_numanswers = "numanswers"; private static final string tag_question = "question"; private static final string tag_question_content = "content"; private static final string tag_question_chosenanswer = "chosenanswer"; private static final string tag_question_answers = "answers"; public jsonarray json; @override protected jsonobject doinbackground(string... params) { jsonparsser jparser = new jsonparsser(); string uri = params[0]; jsonobject json = jparser.getjsonfromuri(uri); return json ; }{ } @override protected void onpostexecute(jsonobject json) { jsonarray resultsset = null; { try { resultsset = json.getjsonarray(tag_results); } catch (jsonexception e) { e.printstacktrace(); } return; }}}
jsonparsser class
public class jsonparsser { static inputstream = null; static jsonobject jobj = null; static string json = ""; edittext et; public jsonparsser () { } public jsonobject getjsonfromuri(string uri) { try{ httpclient client = new defaulthttpclient(); try { uri website = new uri("http://example=" + et.gettext() + "json"); httpget request = new httpget(); request.seturi(website); try { httpresponse response = client.execute(request); httpentity httpentity = response.getentity(); = httpentity.getcontent(); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (urisyntaxexception e) { // todo auto-generated catch block e.printstacktrace(); } return jobj; }finally{} }{ }}
instanceofjsonparsser class
public class jsonparserinstance { jsonparsser jparser = new jsonparsser(); public string uri; jsonobject json = jparser.getjsonfromuri(uri); private static final string tag_results = "resultsset"; private static final string tag_subject = "subject"; private static final string tag_numanswers = "numanswers"; private static final string tag_question = "question"; private static final string tag_question_content = "content"; private static final string tag_question_chosenanswer = "chosenanswer"; private static final string tag_question_answers = "answers"; jsonarray resultsset = null; { try { resultsset = json.getjsonarray(tag_results); for(int = 0; < resultsset.length(); i++){ jsonobject r = resultsset.getjsonobject(i); string subject = r.getstring(tag_subject); string nmanswers = r.getstring(tag_numanswers); jsonobject question = r.getjsonobject(tag_question); string content = question.getstring(tag_question_content); string chosenanswer = question.getstring(tag_question_chosenanswer); string answers = question.getstring(tag_question_answers); } } catch (jsonexception e) { e.printstacktrace(); }}}
uri website = new uri("http://example=" + et.gettext() + "json");
you didn't connect edittext et it's view. et.gettext give error.
uri website = new uri("http://example=" + et.gettext() + "json");
change uri website = uri;
and pass uri while processing doinbackground
like -
string uri = "http://www.example.com/example.json"; jsonobject json = jparser.getjsonfromuri(uri);
Comments
Post a Comment