0

I have a parsed a YouTube GDATA JSON-C file, and as such it is JSON the duration of each video does not have the : in between (it is parsed as 23 or 432 instead of 0:23 or 4:32) how would i split a string before the last 2 characters without knowing the length as it can be anywhere from 2 - 6 strings

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 Answer 1

1

public class string { public static void main(String[] args) {

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

}

Sign up to request clarification or add additional context in comments.

4 Comments

i have tried to add this into my code but still doesnt affect it]
its working. copy addToString(.....) this funcation and when you needed call
so i use string op to display in the text view/
so how do i use with the variable strings displayed in the code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.