올해는 머신러닝이다.
youtube rtsp 구하는 법 본문
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();
}
};
}
'Android > Tip&Tech' 카테고리의 다른 글
android image upload with php (0) | 2011.08.02 |
---|---|
VideoView 전체화면으로 보기 팁 (0) | 2011.07.29 |
안드로이드 이러닝 관련 참고 소스 (0) | 2011.07.24 |
[펌][Android] 많은 버튼의 클릭이벤트 처리 : button onClick() : OnClickListener (1) | 2011.07.23 |
안드로이드 주소록에서 보여지는 퀵액션 같은 걸 붙여보자 (1) | 2011.07.21 |