http://stackoverflow.com/questions/6520313/how-to-get-rtsp-links-android 에서 참조

package jp.ddo.gallop.android;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import jp.ddo.gallop.android.youtube.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends Activity implements Runnable {
 private HttpClient mClient;
 private HttpGet mGetMethod;
 private EditText edittext;
 private Button button;
 private ListView listview;
 private Entries<Entry> youtubeEntries;
 private InputStream is;
 private ProgressDialog progressDialog;
 
    /** Called when the activity is first created. */
    @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
       
  edittext = (EditText) findViewById(R.id.EditText01);
  button = (Button) findViewById(R.id.Button01);
  listview = (ListView) findViewById(R.id.ListView01);
       
  // R.id.Button01 OnClickListener
  button.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // プログレスダイアログ
    progressDialog = new ProgressDialog(Main.this);
    progressDialog.setTitle("YouTube");
    progressDialog.setMessage("検索中・・・");
    progressDialog.setIndeterminate(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();

    // Button01 クリックイベント
    Button01_OnClick();  
   }
  });

  // R.id.ListView01 OnItemClickListener
  listview.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    /*
    Entry entryItem = youtubeEntries.get(position);
    */
   }
  });
 }
   
 // Button01 クリックイベント
 private void Button01_OnClick() {
  String keyword = "";
  String encodeKeyword = "";
  keyword = edittext.getText().toString();

  try {
   encodeKeyword = URLEncoder.encode(keyword, "UTF-8");
   
   // YouTube 検索URL
   String uri = "http://gdata.youtube.com/feeds/api/videos?vq=" + encodeKeyword;
   mClient = new DefaultHttpClient();
   mGetMethod = new HttpGet();
   HttpResponse resp = null;
   
   // Request する URL を設定
   mGetMethod.setURI(new URI(uri));
   
   // METHOD GET で Request 送信
   resp = mClient.execute(mGetMethod);
   
   // Request 結果取得
   if (resp.getStatusLine().getStatusCode() == 200) {
    youtubeEntries = null;
    is = resp.getEntity().getContent();
    
    // YouTubeXMLデータをEntryデータに変換する処理に時間が掛かるためスレッド化してプログレスダイアログを表示
    // スレッドの作成と開始
    Thread thread = new Thread(Main.this);
    thread.start();
   }

  } catch (UnsupportedEncodingException e1) {
   e1.printStackTrace();
  } catch (URISyntaxException e) {
   e.printStackTrace();
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (IllegalStateException e) {
   e.printStackTrace();
  }
 }
   
 // YouTube XML データパース
    @SuppressWarnings("static-access")
 private Entries<Entry> GetYouTubeEntries(InputStream is) throws XmlPullParserException, IOException {
     // XML パース処理
  ・・・
  // この処理にかなり時間が掛かっている
     return retEntries;
    }

    // スレッド処理開始
 @Override
 public void run() {
  // XML Parse
  try {
   // YouTube XML データパース
   youtubeEntries = GetYouTubeEntries(is);
   handler.sendEmptyMessage(0);
   is.close();
  } catch (XmlPullParserException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 private Handler handler = new Handler() {
  public void handleMessage(Message msg) {
   // YouTube用アダプター作成
   YouTubeAdapter youtubeAdapter = new YouTubeAdapter(Main.this);
   
   // R.id.ListView01 にアダプターをセット
   listview.setAdapter(youtubeAdapter);
   
   // R.id.ListView01 のアダプターに Entry データ追加
   for (Entry ytEntry : youtubeEntries) {
    youtubeAdapter.addData(ytEntry);
   }
   
   // プログレスダイアログ終了
   progressDialog.dismiss();
  }
 };
}

+ Recent posts