2013-07-22 37 views
-2

我寫了一個很大的代碼,並在我的應用程序的選項之一是獲取所有該語句的匹配結果得到的特定的YouTube用戶的所有視頻的名字:如何通過編碼

new conn().execute("https://gdata.youtube.com/feeds/api/videos?q=google&v=2&alt=json"); 

這是所有視頻的標題包含「谷歌」如果q =谷歌例如,現在我想檢索所有相關的結果,如果我在YouTube上輸入用戶的名稱,並獲得他所有的視頻..什麼是修改爲了這個聲明去執行那個?

+1

你讀過HTTP ://stackoverflow.com/questions/8967711/how-to-get-the-youtube-videos-by-user?rq = 1? –

+0

你好,哥們。看看這個:http://stackoverflow.com/a/12854159/1405983 –

回答

0

由該更換網址: http://gdata.youtube.com/feeds/api/videos?author=username&v=2&alt=json我可以得到相同的用戶的所有視頻:

new conn().execute("http://gdata.youtube.com/feeds/api/videos?author=username&v=2&alt=json"); 

這是類康涅狄格州:

class conn extends AsyncTask<String, Integer, String>{ 


     @Override 
     protected void onPreExecute() { 
      Log.d("after make conn", "ok"); 
      progress.show(); 
      super.onPreExecute(); 

     } 
     @Override 
     protected String doInBackground(String... arg0) { 
      Log.d("doInBackground", "ok"); 
      String s = GetUrlBody(arg0[0]); 

      return s; 
     } 

     @Override 
     protected void onPostExecute(String result) { 

     try{ 

      Log.d("onPostExecute befor parsing", "ok"); 

     /*************************************************************************/ 

      JSONObject jo =(JSONObject) new JSONTokener(result).nextValue(); 


      JSONObject feed = jo.optJSONObject("feed"); 


      JSONArray ent = feed.optJSONArray("entry"); 

      Log.d(" after parsing before loop", "ok"); 

     /*************************************************************************/ 

     for(int i = 0 ; i<ent.length() ; i++){ 

     String title = ent.optJSONObject(i). 
       optJSONObject("title").optString("$t"); 

     String views = ent.optJSONObject(i).optJSONObject("yt$statistics").optString("viewCount"); 

     String authorName=ent.optJSONObject(i).optJSONArray("author").optJSONObject(0).optJSONObject("name").optString("$t"); 

     String numDisLikes = ent.optJSONObject(i). 
       optJSONObject("yt$rating").optString("numDislikes"); 

     String numLikes = ent.optJSONObject(i). 
       optJSONObject("yt$rating").optString("numLikes"); 

     String description = ent.optJSONObject(i). 
       optJSONObject("media$group").optJSONObject("media$description").optString("$t"); 
     String shortDescribtion=description.substring(0,49); 
     Log.d(" value of url", description); 

     String url=ent.optJSONObject(i).optJSONObject("media$group").optJSONArray("media$thumbnail").optJSONObject(0).optString("url"); 
     Log.d(" value of array", url); 

     String link=ent.optJSONObject(i).optJSONArray("link").optJSONObject(0).optString("href"); 

     Log.d(" after parsing in loop", "ok"); 




     /*************************************************************************/ 



    videoInfo.add("Title:"+title+"\n"+"By:"+authorName+"\n"+shortDescribtion); 


     Log.d(" after parsing in loop after list", "ok"); 

     db.insertRow(title, numLikes, numDisLikes,views, authorName, link, description, url, vedioName); 
     Log.d(" after parsing in loop after insert", "ok"); 




     } 

     Log.d("finish parsing ", "ok"); 
     db.close(); 

     /*************************************************************************/ 

     Listadapter.notifyDataSetChanged(); 


     Log.d(" after notify", "ok"); 

     /*************************************************************************/ 

     }catch(Exception exx) { 


      Log.getStackTraceString(exx.getCause().getCause()); 

     } 

     /*************************************************************************/ 

      progress.dismiss(); 

     /*************************************************************************/ 

      super.onPostExecute(result); 


     } 



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////  

     String GetUrlBody (String Url){ 

      Log.d(" in GetUrlBody", "ok"); 

      HttpClient cli = new DefaultHttpClient(); 


      HttpGet g = new HttpGet(Url); 


      try{ 



      HttpResponse res = cli.execute(g); 


      if(res.getStatusLine().getStatusCode() == 200){ 


       String s =EntityUtils.toString(res.getEntity(), HTTP.UTF_8); 


       return s; 

      }else { 



       return "Not Found"; 


      } 




      }catch(Exception exx){ 



       Log.getStackTraceString(exx.getCause().getCause()); 


      } 

      return "!"; 
     } 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////  



    }