2012-08-22 3 views
0

구문 분석 된 YouTube GDATA JSON-C 파일이 있는데 JSON이므로 각 동영상의 재생 시간에는 :(중간에 0:23 대신 23 또는 432로 구문 분석됩니다. 것 내가 그것을 어느 곳에서든지 2에서 할 수있는 길이 모른 채 지난 2 개 문자 전에 문자열을 분할하는 방법 또는 4시 32분) - 6 문자열ANDROID 분할 변수 문자열

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)); 




    ;}});}} 

답변

1

공용 클래스 문자열 { 공공 정적 무효 메인 (문자열 [] 인수) {

String str= "45"; 
String op=addToString(str, ":"); 
System.out.println(op); 
} 
static String addToString(String str, String ins) { 
    if(str.length()<=2) 
    { 
     str="00"+str; 
    } 
    int i = str.length()-(str.length()-2); 
    return str.substring(0, i) + ins + str.substring(i); 
} 

}

+0

나는이 코드를 내 코드에 추가하려고했지만 여전히 영향을 미치지 않습니다.] – SquiresSquire

+0

작동합니다. 복사 addToString (.....)이 funcation 당신은 그래서 난 문자열 영업 이익은 –

+0

을 필요로 할 때? – SquiresSquire