java - Json Data on Fragment -
i learning android programming , created basic project using android studio.
my application crashes when click on getdata
button.
the mainactivity.java:
package com.cs_infotech.newpathshala; import android.app.activity; import android.app.fragmenttransaction; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.content.context; import android.os.build; import android.os.bundle; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.support.v4.widget.drawerlayout; import android.widget.arrayadapter; import android.widget.textview; import android.widget.toast; public class mainactivity extends actionbaractivity implements navigationdrawerfragment.navigationdrawercallbacks { /** * fragment managing behaviors, interactions , presentation of navigation drawer. */ private navigationdrawerfragment mnavigationdrawerfragment; /** * used store last screen title. use in {@link #restoreactionbar()}. */ private charsequence mtitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnavigationdrawerfragment = (navigationdrawerfragment) getsupportfragmentmanager().findfragmentbyid(r.id.navigation_drawer); mtitle = gettitle(); // set drawer. mnavigationdrawerfragment.setup( r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout)); } @override public void onnavigationdraweritemselected(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new examplefragment(); break; case 1: fragment = new example1fragment(); break; case 2: fragment = new example2fragment(); break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.container, fragment).commit(); } } public void onsectionattached(int number) { switch (number) { case 1: mtitle = "epathshala"; break; case 2: mtitle = "daily collection"; break; case 3: mtitle = "new enrolled"; break; } } public void restoreactionbar() { actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_standard); actionbar.setdisplayshowtitleenabled(true); actionbar.settitle(mtitle); } @override public boolean oncreateoptionsmenu(menu menu) { if (!mnavigationdrawerfragment.isdraweropen()) { // show items in action bar relevant screen // if drawer not showing. otherwise, let drawer // decide show in action bar. getmenuinflater().inflate(r.menu.main, menu); restoreactionbar(); return true; } return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; /** * returns new instance of fragment given section * number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } @override public void onattach(activity activity) { super.onattach(activity); ((mainactivity) activity).onsectionattached( getarguments().getint(arg_section_number)); } } }
test1.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <button android:id="@+id/getdata" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:layout_marginbottom="23dp" android:text="get data" /> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/getdata" /> <textview android:id="@+id/vers" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/api" android:layout_width="match_parent" android:layout_height="wrap_content" /> </linearlayout>
example1fragment.java:
package com.cs_infotech.newpathshala; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.button; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; import android.widget.toast; import com.cs_infotech.newpathshala.r; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; public class example1fragment extends fragment { listview list; textview ver; textview name; textview api; button btngetdata; arraylist<hashmap<string, string>> oslist = new arraylist<hashmap<string, string>>(); //url json array private static string url = "http://api.learn2crack.com/android/jsonos/"; //json node names private static final string tag_os = "android"; private static final string tag_ver = "ver"; private static final string tag_name = "name"; private static final string tag_api = "api"; jsonarray android = null; private view v; public example1fragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.test1, container, false); oslist = new arraylist<hashmap<string, string>>(); btngetdata = (button) rootview.findviewbyid(r.id.getdata); v=rootview; btngetdata.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { new jsonparse().execute(); //toast.maketext(getactivity(), "you clicked @ " , toast.length_short).show(); } }); return rootview; } private class jsonparse extends asynctask<string, string, jsonobject> { private progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); name = (textview) v.findviewbyid(r.id.name); ver = (textview) v.findviewbyid(r.id.vers); api = (textview) v.findviewbyid(r.id.api); pdialog = new progressdialog(getactivity()); pdialog.setmessage("getting data ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... args) { jsonparser jparser = new jsonparser(); // getting json url jsonobject json = jparser.getjsonfromurl("http://api.learn2crack.com/android/jsonos/"); return json; } @override protected void onpostexecute(jsonobject json) { pdialog.dismiss(); try { // getting json array url android = json.getjsonarray(tag_os); for(int = 0; < android.length(); i++){ jsonobject c = android.getjsonobject(i); // storing json item in variable string ver = c.getstring(tag_ver); string name = c.getstring(tag_name); string api = c.getstring(tag_api); // adding value hashmap key => value hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_ver, ver); map.put(tag_name, name); map.put(tag_api, api); oslist.add(map); list=(listview) v.findviewbyid(r.id.list); listadapter adapter = new simpleadapter(getactivity(), oslist, r.layout.list_v, new string[] { tag_ver,tag_name, tag_api }, new int[] { r.id.vers,r.id.name, r.id.api}); list.setadapter(adapter); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { toast.maketext(getactivity(), "you clicked @ " + oslist.get(+position).get("name"), toast.length_short).show(); } }); } } catch (jsonexception e) { e.printstacktrace(); } } } }
logcat:
04-10 10:12:52.253 30126-30126/com.cs_infotech.newpathshala d/activitythread﹕ handlebindapplication:com.cs_infotech.newpathshala 04-10 10:12:52.343 30126-30126/com.cs_infotech.newpathshala w/applicationpackagemanager﹕ getcscpackageitemtext() 04-10 10:12:52.343 30126-30126/com.cs_infotech.newpathshala d/displaymanager﹕ displaymanager() 04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala i/dalvikvm﹕ not find method android.view.viewgroup.onnestedscrollaccepted, referenced method android.support.v7.internal.widget.actionbaroverlaylayout.onnestedscrollaccepted 04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala w/dalvikvm﹕ vfy: unable resolve virtual method 11359: landroid/view/viewgroup;.onnestedscrollaccepted (landroid/view/view;landroid/view/view;i)v 04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala d/dalvikvm﹕ vfy: replacing opcode 0x6f @ 0x0000 04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala i/dalvikvm﹕ not find method android.view.viewgroup.onstopnestedscroll, referenced method android.support.v7.internal.widget.actionbaroverlaylayout.onstopnestedscroll 04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala w/dalvikvm﹕ vfy: unable resolve virtual method 11365: landroid/view/viewgroup;.onstopnestedscroll (landroid/view/view;)v 04-10 10:12:52.423 30126-30126/com.cs_infotech.newpathshala d/dalvikvm﹕ vfy: replacing opcode 0x6f @ 0x0000 04-10 10:12:52.433 30126-30126/com.cs_infotech.newpathshala i/dalvikvm﹕ not find method android.support.v7.internal.widget.actionbaroverlaylayout.stopnestedscroll, referenced method android.support.v7.internal.widget.actionbaroverlaylayout.sethideoncontentscrollenabled 04-10 10:12:52.433 30126-30126/com.cs_infotech.newpathshala w/dalvikvm﹕ vfy: unable resolve virtual method 9053: landroid/support/v7/internal/widget/actionbaroverlaylayout;.stopnestedscroll ()v 04-10 10:12:52.433 30126-30126/com.cs_infotech.newpathshala d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x000e 04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala i/dalvikvm﹕ not find method android.content.res.typedarray.getchangingconfigurations, referenced method android.support.v7.internal.widget.tinttypedarray.getchangingconfigurations 04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala w/dalvikvm﹕ vfy: unable resolve virtual method 374: landroid/content/res/typedarray;.getchangingconfigurations ()i 04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x0002 04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala i/dalvikvm﹕ not find method android.content.res.typedarray.gettype, referenced method android.support.v7.internal.widget.tinttypedarray.gettype 04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala w/dalvikvm﹕ vfy: unable resolve virtual method 396: landroid/content/res/typedarray;.gettype (i)i 04-10 10:12:52.443 30126-30126/com.cs_infotech.newpathshala d/dalvikvm﹕ vfy: replacing opcode 0x6e @ 0x0002 04-10 10:12:52.834 30126-30126/com.cs_infotech.newpathshala d/openglrenderer﹕ enabling debug mode 0 04-10 10:12:52.934 30126-30126/com.cs_infotech.newpathshala i/timeline﹕ timeline: activity_idle id: android.os.binderproxy@41e049e0 time:9101016 04-10 10:15:04.772 30126-30126/com.cs_infotech.newpathshala i/timeline﹕ timeline: activity_idle id: android.os.binderproxy@41e049e0 time:9232856 04-10 10:15:11.359 30126-30656/com.cs_infotech.newpathshala w/dalvikvm﹕ threadid=11: thread exiting uncaught exception (group=0x416c8bc0) 04-10 10:15:11.379 30126-30656/com.cs_infotech.newpathshala e/androidruntime﹕ fatal exception: asynctask #1 process: com.cs_infotech.newpathshala, pid: 30126 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:300) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) @ java.util.concurrent.futuretask.setexception(futuretask.java:222) @ java.util.concurrent.futuretask.run(futuretask.java:242) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) caused by: java.lang.securityexception: permission denied (missing internet permission?) @ java.net.inetaddress.lookuphostbyname(inetaddress.java:418) @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) @ java.net.inetaddress.getallbyname(inetaddress.java:214) @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) @ com.cs_infotech.newpathshala.jsonparser.getjsonfromurl(jsonparser.java:44) @ com.cs_infotech.newpathshala.example1fragment$jsonparse.doinbackground(example1fragment.java:80) @ com.cs_infotech.newpathshala.example1fragment$jsonparse.doinbackground(example1fragment.java:62) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237) at android.os.asynctask$serialexecutor$1.run(asynctask.java:231) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) at java.lang.thread.run(thread.java:841) caused by: libcore.io.gaiexception: getaddrinfo failed: eai_nodata (no address associated hostname) @ libcore.io.posix.getaddrinfo(native method) @ libcore.io.forwardingos.getaddrinfo(forwardingos.java:61) @ java.net.inetaddress.lookuphostbyname(inetaddress.java:405) at java.net.inetaddress.getallbynameimpl(inetaddress.java:236) at java.net.inetaddress.getallbyname(inetaddress.java:214) at org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) at org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) at org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) at org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) at com.cs_infotech.newpathshala.jsonparser.getjsonfromurl(jsonparser.java:44) at com.cs_infotech.newpathshala.example1fragment$jsonparse.doinbackground(example1fragment.java:80) at com.cs_infotech.newpathshala.example1fragment$jsonparse.doinbackground(example1fragment.java:62) at android.os.asynctask$2.call(asynctask.java:288) at java.util.concurrent.futuretask.run(futuretask.java:237) at android.os.asynctask$serialexecutor$1.run(asynctask.java:231) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) at java.lang.thread.run(thread.java:841) caused by: libcore.io.errnoexception: getaddrinfo failed: eacces (permission denied) at libcore.io.posix.getaddrinfo(native method) at libcore.io.forwardingos.getaddrinfo(forwardingos.java:61) at java.net.inetaddress.lookuphostbyname(inetaddress.java:405) at java.net.inetaddress.getallbynameimpl(inetaddress.java:236) at java.net.inetaddress.getallbyname(inetaddress.java:214) at org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) at org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) at org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) at org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) at org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) at com.cs_infotech.newpathshala.jsonparser.getjsonfromurl(jsonparser.java:44) at com.cs_infotech.newpathshala.example1fragment$jsonparse.doinbackground(example1fragment.java:80) at com.cs_infotech.newpathshala.example1fragment$jsonparse.doinbackground(example1fragment.java:62) at android.os.asynctask$2.call(asynctask.java:288) at java.util.concurrent.futuretask.run(futuretask.java:237) at android.os.asynctask$serialexecutor$1.run(asynctask.java:231) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) at java.lang.thread.run(thread.java:841) 04-10 10:15:12.330 30126-30126/com.cs_infotech.newpathshala e/openglrenderer﹕ sfeffectcache:clear(), msize = 0 04-10 10:15:12.360 30126-30126/com.cs_infotech.newpathshala e/windowmanager﹕ android.view.windowleaked: activity com.cs_infotech.newpathshala.mainactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview{41e91890 v.e..... r......d 0,0-471,144} added here @ android.view.viewrootimpl.<init>(viewrootimpl.java:388) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:248) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) @ android.app.dialog.show(dialog.java:286) @ com.cs_infotech.newpathshala.example1fragment$jsonparse.onpreexecute(example1fragment.java:74) @ android.os.asynctask.executeonexecutor(asynctask.java:587) @ android.os.asynctask.execute(asynctask.java:535) @ com.cs_infotech.newpathshala.example1fragment$1.onclick(example1fragment.java:56) @ android.view.view.performclick(view.java:4508) @ android.view.view$performclick.run(view.java:18675) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5584) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1268) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1084) @ dalvik.system.nativestart.main(native method) 04-10 10:15:14.992 30126-30656/com.cs_infotech.newpathshala i/process﹕ sending signal. pid: 30126 sig: 9
how can resolve this?