package com.example.testsherlock_viewpager; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import org.apache.http.HttpEntity; 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.app.Activity; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.view.MenuInflater; public class FragmentVideo extends SherlockFragment { Activity activity; int data_size = 0; private ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>(); private HashMap<String, String> map; ListView listItem; String url = "http://query.yahooapis.com/v1/public/yql?q=select%20title.content%2C%20link.href%20from%20feed%20where%20url%3D%22https%3A%2F%2Fgdata.youtube.com%2Ffeeds%2Fapi%2Fusers%2Fyannivideos%2Fuploads%3Fmax-results%3D50%22%20and%20link.rel%3D%22alternate%22&format=json&callback="; @Override public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); } @Override public SherlockFragmentActivity getSherlockActivity() { return super.getSherlockActivity(); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_video, container, false); Log.d("VLOG", "onCreateView"); // get xml data form yql if (checkNetworkStatus()) { new LoadContentAsync().execute(); } else { Toast.makeText(activity.getBaseContext(), "No network connection!",Toast.LENGTH_SHORT).show(); } return view; } @Override public void onAttach(Activity a) { // TODO Auto-generated method stub super.onAttach(a); activity = a; } public class LoadContentAsync extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { // load json data try { JSONObject json_data = new JSONObject(getJSONUrl(url)); JSONObject json_query = json_data.getJSONObject("query"); JSONObject json_result = json_query.getJSONObject("results"); JSONArray json_entry = json_result.getJSONArray("entry"); Log.d("JSON", String.valueOf(json_entry.length())); for (int i = 0; i < json_entry.length(); i++) { JSONObject c = json_entry.getJSONObject(i); Log.d("JSON", c.getString("title").toString()); Log.d("JSON", c.getJSONObject("link").getString("href") .toString()); String link = c.getJSONObject("link").getString("href") .toString(); String[] fragments = link.split("&"); String[] videoid = fragments[0].split("="); Log.d("JSON", videoid[1]); // put into hashmap map = new HashMap<String, String>(); map.put("title", c.getString("title")); map.put("link", c.getJSONObject("link").getString("href")); map.put("videoid", videoid[1]); MyArrList.add(map); } data_size = json_entry.length(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub ShowResult(MyArrList); activity.setProgressBarIndeterminateVisibility(false); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); MyArrList.clear(); activity.setProgressBarIndeterminateVisibility(true); } } public String getJSONUrl(String url) { StringBuilder str = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { // Download OK HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { str.append(line); } } else { Log.e("Log", "Failed to download file.."); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return str.toString(); } public void ShowResult(ArrayList<HashMap<String, String>> myArrList) { try { listItem = (ListView) activity.findViewById(R.id.listItem); LazyAdapter adapter = new LazyAdapter(activity, MyArrList); listItem.setAdapter(adapter); listItem.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Intent fanPageIntent = new Intent(Intent.ACTION_VIEW); fanPageIntent.setType("text/url"); fanPageIntent.setData(Uri.parse(MyArrList.get(arg2).get( "link"))); startActivity(fanPageIntent); } }); } catch (Exception e) { Toast.makeText(activity.getBaseContext(), "Cannot connect to server!", Toast.LENGTH_SHORT).show(); } } public boolean checkNetworkStatus() { ConnectivityManager connectivityManager = (ConnectivityManager) activity .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo(); return activeNetworkInfo != null; } }