I have been trying to find a working example of "Parsing Youtube Gdata JSON" in Android to populate a listview.
I have been reading this question, and I alm
Your activity class does not extend ListActivity
. Hence the errors 3 and 4
You should use AsyncTask
for network related operation. You cannot run network related operation on the main ui thread.
You should use lazy loading for images. But for sample i have use a ArrayList of bitmaps for thumbnail. You might run into memory leaks. So use lazy loading. Modify the below according to your requirement.
Example:
public class MainActivity extends Activity {
Button b;
ListView lv;
ArrayList msg = new ArrayList();
ArrayList title = new ArrayList();
ArrayList thumb = new ArrayList();
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Loading..");
b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TheTask().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 void getData()
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?q=random&max-results=50&v=2&alt=jsonc");
// HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/users/mbbangalore/uploads?v=2&alt=jsonc");
try
{
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response=EntityUtils.toString(resEntity); // content will be consume only once
JSONObject json = new JSONObject(_response);
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title1 = jsonObject.getString("title");
title.add(title1);
String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault");
URL url1 = new URL(thumbUrl);
Bitmap bmp = BitmapFactory.decodeStream(url1.openConnection().getInputStream());
thumb.add(bmp);
String url;
try {
url = jsonObject.getJSONObject("player").getString("default");
msg.add(url);
} catch (JSONException ignore) {
}
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
}
class TheTask extends AsyncTask
{
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
YouTubeAdapter you = new YouTubeAdapter(MainActivity.this,msg,title,thumb);
lv.setAdapter(you);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
getData();
return null;
}
}
}
activity_main.xml
YouTubeAdapter
public class YouTubeAdapter extends BaseAdapter{
Context mContext;
Intent intent;
LayoutInflater mInflater;
ArrayList mVideo= new ArrayList();
ArrayList mTitle= new ArrayList();
ArrayList mThumb= new ArrayList();
public YouTubeAdapter(Context context,ArrayList a,ArrayList title,ArrayList thumb) {
mContext=context;
mVideo=a;
mTitle = title;
mThumb= thumb;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mVideo.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View arg1, ViewGroup arg2) {
ViewHolder vh;
if(arg1==null)
{
arg1=mInflater.inflate(R.layout.video, null);
vh= new ViewHolder();
vh.tv=(TextView)arg1.findViewById(R.id.tvv);
vh.iv=(ImageView)arg1.findViewById(R.id.ivv);
arg1.setTag(vh);
}
else
{
vh= (ViewHolder)arg1.getTag();
}
vh.tv.setText(mTitle.get(position));
vh.iv.setImageBitmap(mThumb.get(position));
return arg1;
}
static class ViewHolder
{
TextView tv;
ImageView iv;
}
}
video.xml
Snap shot