Skip to content

Instantly share code, notes, and snippets.

@iulianpw
Created July 25, 2013 06:54
Show Gist options
  • Save iulianpw/6077433 to your computer and use it in GitHub Desktop.
Save iulianpw/6077433 to your computer and use it in GitHub Desktop.
package com.twistedequations.youtubevideolist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView videoList;
ArrayList<String> videoArrayList = new ArrayList<String>();
ArrayAdapter<String> videoAdapter;
Context context;
String feedUrl = "http://gdata.youtube.com/feeds/api/users/twistedequations/uploads?v=2&alt=jsonc&start-index=1&max-results=30";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
videoList = (ListView) findViewById(R.id.videoList);
videoAdapter = new ArrayAdapter<String>(this, R.layout.video_list_item, videoArrayList);
videoList.setAdapter(videoAdapter);
VideoListTask loaderTask = new VideoListTask();
loaderTask.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class VideoListTask extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
@Override
protected void onPreExecute()
{
dialog = new ProgressDialog(context);
dialog.setTitle("Loading Videos");
dialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params)
{
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(feedUrl);
try
{
HttpResponse responce = client.execute(getRequest);
StatusLine statusLine = responce.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200)
{
return null;
}
InputStream jsonStream = responce.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(jsonStream));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine())!=null)
{
builder.append(line);
}
String jsonData = builder.toString();
JSONObject json = new JSONObject(jsonData);
JSONObject data = json.getJSONObject("data");
JSONArray items = data.getJSONArray("items");
for(int i =0; i<items.length(); i++)
{
JSONObject video = items.getJSONObject(i);
videoArrayList.add(video.getString("title"));
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
videoAdapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment