ANDROID拆分变量字符串

时间:2012-08-22 06:20:57

标签: android json split gdata

我有一个解析过的YouTube GDATA JSON-C文件,因此它是JSON,每个视频的持续时间没有:介于两者之间(它被解析为23或432而不是0:23或4:32如何在不知道长度的情况下在最后2个字符之前拆分字符串,因为它可以是2到6个字符串的任何地方

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 个答案:

答案 0 :(得分:1)

公共类字符串{     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);
}

}