i want display titles of videos contains specific substring such example videoname="apple"
:
so in order perform task wrote code retrieve json file , entries related name: ("https://gdata.youtube.com/feeds/api/videos?q="+videoname+"&v=2&alt=json") unfortunately had exception @ statement:
httpresponse res = cli.execute(g);
this function wrote json file:
string geturlbody (string url ){ log.d("s18", "ok"); httpclient cli = new defaulthttpclient(); log.d("s19", "ok"); httpget g = new httpget(url); log.d("s20", "ok"); try{ log.d("s21", "ok"); httpresponse res = cli.execute(g); log.d("s22", "ok"); if(res.getstatusline().getstatuscode() == 200){ log.d("s23", "ok"); string s = entityutils.tostring(res.getentity(), http.utf_8); log.d("s24", "ok"); return s; }else { log.d("s25", "ok"); return "not found"; } }catch(exception exx){ log.d("s26", "ok"); log.d("s27", exx.getmessage()); } return null; }
all code:
package com.example.task_10_vedioserach; import java.util.arraylist; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.protocol.http; import org.apache.http.util.entityutils; import org.json.jsonarray; import org.json.jsonobject; import org.json.jsontokener; import android.os.asynctask; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.app.progressdialog; import android.util.log; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.imagebutton; import android.widget.listview; public class mainactivity extends activity { listview vediolist; imagebutton search; button history; edittext title; ///////////////// arraylist<string > vl; arrayadapter< string > ad ; progressdialog pd ; ///////////////// @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); vediolist = (listview) findviewbyid(r.id.vediolist); search = (imagebutton) findviewbyid(r.id.search); history = (button) findviewbyid(r.id.history); title=(edittext) findviewbyid(r.id.vedioname); ///////////////////////////////////////////////////////////////// vl = new arraylist<string>(); ad = new arrayadapter<string>(this, android.r.layout.simple_list_item_1 , android.r.id.text1,vl); ////////////////////////////////////////////////////////////// log.d("s0", "ok"); search.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final string vedioname=title.gettext().tostring(); log.d("s1", "ok"); new conn().execute("https://gdata.youtube.com/feeds/api/videos?q="+vedioname+"&v=2&alt=json"); log.d("s2", "ok"); } }); pd = new progressdialog(this); pd.setmessage("wait loading .... "); pd.setcancelable(false); vediolist.setadapter(ad); } class conn extends asynctask<string, integer, string>{ @override protected void onpreexecute() { log.d("s3", "ok"); pd.show(); super.onpreexecute(); log.d("s4", "ok"); } @override protected string doinbackground(string... arg0) { log.d("s5", "ok"); string s = geturlbody(arg0[0]); log.d("s6", "ok"); return s; } @override protected void onpostexecute(string result) { log.d("s7", "ok"); try{ log.d("s8", "ok"); jsonobject jo =(jsonobject) new jsontokener(result).nextvalue(); log.d("s9", "ok"); jsonobject feed = jo.optjsonobject("feed"); log.d("s10", "ok"); jsonarray ent = feed.optjsonarray("entry"); log.d("s11", "ok"); for(int = 0 ; i<ent.length() ; i++){ string ti = ent.getjsonobject(i). getjsonobject("title").getstring("$t"); vl.add(ti); } log.d("s12", "ok"); ad.notifydatasetchanged(); log.d("s13", "ok"); }catch(exception exx) { log.d("s14", "ok"); } log.d("s15", "ok"); pd.dismiss(); log.d("s16", "ok"); super.onpostexecute(result); log.d("s17", "ok"); } string geturlbody (string url ){ log.d("s18", "ok"); httpclient cli = new defaulthttpclient(); log.d("s19", "ok"); httpget g = new httpget(url); log.d("s20", "ok"); try{ log.d("s21", "ok"); httpresponse res = cli.execute(g); log.d("s22", "ok"); if(res.getstatusline().getstatuscode() == 200){ log.d("s23", "ok"); string s = entityutils.tostring(res.getentity(), http.utf_8); log.d("s24", "ok"); return s; }else { log.d("s25", "ok"); return "not found"; } }catch(exception exx){ log.d("s26", "ok"); log.d("s27", exx.getmessage()); } return null; } }
this full stacktrace: here
i recommend using gson , jackson. links bellow:
gson: https://sites.google.com/site/gson/gson-user-guide http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ http://albertattard.blogspot.co.at/2009/06/practical-example-of-gson.html
jackson: http://jackson.codehaus.org/tutorial http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
now have understand using these tools not "for lazy". built , fast mappers. job done quicker , easier algorithm / method alone can think of in short amount of time. hope helps!
Comments
Post a Comment