android - How can I parse json before post data for my listview adapter -
i m trying make json parser android application.
i need post data after m getting json object in while listview adapter. m using code getting error. tried because change many times cant that. (i can json data, in case, want make post before json parse.) how should edit code? help, documents.
i m getting error :
java.lang.runtimeexception: error occured while executing doinbackground()
this code :
public class veritabanikayit extends asynctask<string, integer, jsonobject> { private progressdialog pdialog; jsonobject json2; @override protected void onpreexecute() { super.onpreexecute(); yaydescription = (textview)findviewbyid(r.id.yaydescription); yaydistance = (textview)findviewbyid(r.id.yaydistance); yaycreatedtime = (textview)findviewbyid(r.id.yaycreatedtime); pdialog = new progressdialog(homelistview.this); pdialog.setmessage("wait min ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected jsonobject doinbackground(string... params) { // todo auto-generated method stub postdata(params[0],params[1],params[2]); jsonparser jparser = new jsonparser(); // getting json url url = "11.22.11.22/messages/list"; system.out.println("thisismyurl"+url); json2 = jparser.getjsonfromurl(url); return json2; } protected void onpostexecute(jsonobject result){ // pb.setvisibility(view.gone); } protected void onprogressupdate(integer... progress){ // pb.setprogress(progress[0]); } public void postdata(string personname, string username, string email) { // create new httpclient , post header httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://11.22.11.22/messages/list"); try { // add data list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("token", "zyob3xkdnv")); namevaluepairs.add(new basicnamevaluepair("lat", "37")); namevaluepairs.add(new basicnamevaluepair("long", "-122")); httppost.setentity(new urlencodedformentity(namevaluepairs)); // execute http post request httpresponse response = httpclient.execute(httppost); // string json2 = entityutils.tostring(response.getentity()); /* jsonobject jobject = null; try { jobject = new jsonobject(json); system.out.println("jobject"+jobject); string tokenvalue=jobject.getstring("token"); system.out.println("tokenvalue"+tokenvalue); edit = pref.edit(); edit.putstring("sharedusername", personname.tostring()); edit.putstring("sharedemail", email.tostring()); edit.putstring("sharedtoken", tokenvalue); edit.commit(); } catch (jsonexception e) { e.printstacktrace(); } */ try { jsonobject json2; jsonparser jparser = new jsonparser(); json2 = jparser.getjsonfromurl(url); // getting json array url android = json2.getjsonarray(tag_os); for(int = 0; < android.length(); i++){ jsonobject c = android.getjsonobject(i); // string json item in variable string yaydescription = c.getstring(text); string yaydistance = c.getstring(distance); string yaycreatedtime = c.getstring(created_at); // adding value hashmap key => value hashmap<string, string> map = new hashmap<string, string>(); map.put(text, yaydescription); map.put(distance, yaydistance); map.put(created_at, yaycreatedtime); oslist.add(map); myownpost =(listview)findviewbyid(r.id.listview); myownpost.setchoicemode(listview.choice_mode_single); // myownpost.setrotation(-90); listadapter adapter = new simpleadapter(homelistview.this, oslist, r.layout.homelistviewitem, new string[] { text, distance, created_at }, new int[] { r.id.yaydescription,r.id.yaydistance, r.id.yaycreatedtime }); myownpost.setadapter(adapter); myownpost.setonitemclicklistener(new adapterview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string whatissoundname = oslist.get(+position).get("yaydescription"); toast.maketext(homelistview.this, "you clicked @ " + position, toast.length_short).show(); } }); } } catch (jsonexception e) { e.printstacktrace(); } pdialog.dismiss(); int responsecode = response.getstatusline().getstatuscode(); } catch (clientprotocolexception e) { // todo auto-generated catch block } catch (ioexception e) { // todo auto-generated catch block } } }
try put code in
@override protected void onpostexecute(string result) { myownpost =(listview)findviewbyid(r.id.listview); myownpost.setchoicemode(listview.choice_mode_single);listadapter adapter = new simpleadapter(homelistview.this, oslist, r.layout.homelistviewitem, new string[] { text, distance, created_at }, new int[] { r.id.yaydescription,r.id.yaydistance, r.id.yaycreatedtime }); myownpost.setadapter(adapter); myownpost.setonitemclicklistener(new adapterview.onitemclicklistener(){ @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string whatissoundname = oslist.get(+position).get("yaydescription"); toast.maketext(homelistview.this, "you clicked @ " + position, toast.length_short).show(); } }); }
Comments
Post a Comment