2014-10-17 62 views
0

這個程序應該顯示的縮略圖,標題和播放列表選擇的每個視頻的持續時間,但縮略圖保持白色 應用程序的工作,但它不顯示視頻的縮略圖播放列表無法顯示來自YouTube的縮略圖?

怎麼可以做什麼?你有任何解決方案?

這是我的代碼

public class MainActivity extends Activity { 

ImageView mainThumb; 
TextView mainTitle; 
TextView mainTime; 
LinearLayout videos; 
ArrayList<String> links; 

/** Called when the activity is first created. */ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.videos); 

    new ParseVideoDataTask().execute(); 
    mainThumb = (ImageView) findViewById(R.id.mainThumb); 
    mainTitle = (TextView) findViewById(R.id.mainTitle); 
    mainTime = (TextView) findViewById(R.id.mainTime); 
    videos = (LinearLayout) findViewById(R.id.videos); 

} 

private class ParseVideoDataTask extends AsyncTask<String, String, String> { 
    int count = 0; 

    @Override 
    protected String doInBackground(String... params) { 
     URL jsonURL; 
     URLConnection jc; 
     links = new ArrayList<String>(); 
     try { 
      jsonURL = new URL("http://gdata.youtube.com/feeds/api/playlists/" + 
        "PL-7t9DoIELCRF7F7bZvvBwO1yvHRFsJiu" + 
       "?v=2&alt=jsonc"); 
      //PL_VGbflD64WSR1MBcpWlNwsY0lRNVuJ3r 
      jc = jsonURL.openConnection(); 
      InputStream is = jc.getInputStream(); 
      String jsonTxt = IOUtils.toString(is); 
      JSONObject jj = new JSONObject(jsonTxt); 
      JSONObject jdata = jj.getJSONObject("data"); 
      JSONArray aitems = jdata.getJSONArray("items"); 
      for (int i=0;i<aitems.length();i++) { 
       JSONObject item = aitems.getJSONObject(i); 
       JSONObject video = item.getJSONObject("video"); 
       String title = video.getString("title"); 
       JSONObject player = video.getJSONObject("player"); 
       String link = player.getString("default"); 
       String length = video.getString("duration"); 
       JSONObject thumbnail = video.getJSONObject("thumbnail"); 
       String thumbnailUrl = thumbnail.getString("hqDefault"); 
       String[] deets = new String[4]; 
       deets[0] = title; 
       deets[1] = thumbnailUrl; 
       deets[2] = length; 
       links.add(link); 
       publishProgress(deets); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }  
     return null; 
    }  

    @Override 
    protected void onProgressUpdate(final String... deets) { 
     count++; 
     if (count == 1) { 
      MainActivity.setImageFromUrl(deets[1], mainThumb, MainActivity.this); 
      mainTitle.setText(deets[0]); 
      mainTime.setText(formatLength(deets[2])); 
      mainThumb.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(1))); 
        startActivity(i); 
       } 
      }); 
     } else { 
      LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View video = layoutInflater.inflate(R.layout.video, null); 
      ImageView thumb = (ImageView) video.findViewById(R.id.thumb); 
      TextView title = (TextView) video.findViewById(R.id.title); 
      TextView time = (TextView) video.findViewById(R.id.time); 
      MainActivity.setImageFromUrl(deets[1], thumb, MainActivity.this); 
      title.setText(deets[0]); 
      time.setText(formatLength(deets[2])); 
      video.setPadding(20, 20, 20, 20); 
      videos.addView(video); 
      video.setId(count-1); 
      video.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(v.getId()))); 
        startActivity(i); 
       } 
      }); 
     } 
    } 
} 

private CharSequence formatLength(String secs) { 
    int secsIn = Integer.parseInt(secs); 
    int hours = secsIn/3600, 
      remainder = secsIn % 3600, 
      minutes = remainder/60, 
      seconds = remainder % 60; 

      return ((minutes < 10 ? "0" : "") + minutes 
      + ":" + (seconds< 10 ? "0" : "") + seconds); 
} 



public static void setImageFromUrl(String string, ImageView mainThumb2, 
     MainActivity mainActivity) { 

} 



@Override 
public void onDestroy() { 
    super.onDestroy(); 
    for (int i=0;i<videos.getChildCount();i++) { 
     View v = videos.getChildAt(i); 
     if (v instanceof ImageView) { 
      ImageView iv = (ImageView) v;   
      ((BitmapDrawable)iv.getDrawable()).getBitmap().recycle(); 
     } 
    } 
} 

}

+0

我還沒有給你答案,但我建議你使用像Retrofit http://square.github.io/retrofit/這樣的Http庫,它可能會讓你的生活在處理Http時容易很多端點。 :) – 2014-10-17 17:50:20

回答

0

使用Picasso把圖像的URL像這樣:

Picasso.withContext(this).load(thumbnailUrl).into(myImageView); 

要做到這一點,你需要在返回的JSON對象doInBackground方法,或者使用Retrofit(就像我在上面的評論中提到的那樣)來獲得一個漂亮的POJO並返回它。然後在postExecute中,您可以使用JSON(或POJO)中的縮略圖URL並使用上面的Picasso進行設置。畢加索將負責爲您下載和緩存圖像並將其加載到圖像視圖中。

+0

我在哪裏可以插入這行代碼?謝謝 – john 2014-10-17 17:56:05

+0

我剛更新了我的評論和更多細節。 – 2014-10-17 17:57:38

+0

所以你需要在你的AsyncTask中實現onPostExecute。不是從doInBackground返回null,而是返回JSON對象(如果使用Retrofit/etc,則返回POJO)。然後,在onPostExecute中,您可以通過Picasso加載圖像。 – 2014-10-17 17:59:13