YouTube GDATA JSON-C ファイルを解析しましたが、JSON であるため、各ビデオの長さの間に : はありません (0:23 または 4:32 ではなく 23 または 432 として解析されます)。 2〜6文字列のどこにでもある可能性があるため、長さを知らずに最後の2文字の前で文字列を分割します
public class Uploads extends Activity {
static final String KEY_VIDEOITEM = "item";
static final String KEY_VIDEOID = "id";
static final String KEY_VIDEOTITLE = "title";
static final String KEY_VIDEODESCRIPTION = "description";
static final String KEY_VIDEOCOUNT = "viewCount";
static final String KEY_VIDEODURATION = "duration";
static final String KEY_VIDEODURATIONFORMATTED = "duration";
static final String KEY_VIDEOTHUMB_URL = "thumb_url";
static final String KEY_VIDEOURL = "videourl";
static String Duration = "duration";
ListView list;
org.scouts.android.videos.LazyAdapter adapter;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.video_lsitview);
TextView lblTitle = (TextView) findViewById(R.id.actionbar);
lblTitle.setText("Uploads");
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//Get the data (see above)
//JSONObject json = getJSON.getJSONfromURL("http://gdata.youtube.com/feeds/api/users/worldscouting/playlists?alt=jsonc");
HttpClient client = new DefaultHttpClient();
// Perform a GET request to YouTube for a JSON list of all the videos by a specific user
HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/users/worldscouting/uploads?v=2&alt=jsonc");
// Get the response that YouTube sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Convert this response into a readable string
String jsonString = null;
try {
jsonString = StreamUtils.convertToString(response.getEntity().getContent());
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create a JSON object that we can use from the String
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//JSONArray earthquakes = json.getJSONObject("data").getJSONArray("items");
JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
//Loop the Array
for(int i=0;i < jsonArray.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonObject = jsonArray.getJSONObject(i);
//JSONObject uploads = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("videoid", jsonObject.getString("id"));
map.put(KEY_VIDEOTITLE, jsonObject.getString(KEY_VIDEOTITLE));
map.put(KEY_VIDEODESCRIPTION, jsonObject.getString(KEY_VIDEODESCRIPTION));
map.put(KEY_VIDEOCOUNT, "Views: "+jsonObject.getString(KEY_VIDEOCOUNT));
map.put(KEY_VIDEODURATION, jsonObject.getString(KEY_VIDEODURATION));
map.put(KEY_VIDEOURL, jsonObject.getJSONObject("player").getString("mobile"));
map.put(KEY_VIDEOTHUMB_URL, jsonObject.getJSONObject("thumbnail").getString("sqDefault"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
list=(ListView)findViewById(R.id.videolist);
adapter=new org.scouts.android.videos.LazyAdapter(this, mylist);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String videourl = ((TextView) view.findViewById(R.id.video_id)).getText().toString();
Uri uri = Uri.parse(videourl);
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
;}});}}