ANDROID拆分变量字符串

ANDROID拆分变量字符串,android,json,split,gdata,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"; sta

我有一个解析过的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 ) );




    ;}});}}
公共类上载扩展活动{
静态最终字符串键\u VIDEOITEM=“item”;
静态最终字符串键\u VIDEOID=“id”;
静态最终字符串键\u VIDEOTITLE=“title”;
静态最终字符串键\u VIDEODESCRIPTION=“description”;
静态最终字符串键\u VIDEOCOUNT=“viewCount”;
静态最终字符串键\u VIDEODURATION=“duration”;
静态最终字符串键\u VIDEODURATIONFORMATTED=“duration”;
静态最终字符串键\u VIDEOTHUMB\u URL=“thumb\u URL”;
静态最终字符串键\u VIDEOURL=“VIDEOURL”;
静态字符串Duration=“Duration”;
列表视图列表;
org.scouts.android.videos.LazyAdapter;
/**在首次创建活动时调用*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.video_lsitview);
TextView lblTitle=(TextView)findViewById(R.id.actionbar);
lblTitle.setText(“上传”);
ArrayList mylist=新的ArrayList();
//获取数据(见上文)
//JSONObject json=getJSON.getJSONfromURL(“http://gdata.youtube.com/feeds/api/users/worldscouting/playlists?alt=jsonc");
HttpClient=new DefaultHttpClient();
//向YouTube执行GET请求,获取特定用户所有视频的JSON列表
HttpUriRequest请求=新的HttpGet(“http://gdata.youtube.com/feeds/api/users/worldscouting/uploads?v=2&alt=jsonc");
//获取YouTube发回的响应
HttpResponse响应=null;
试一试{
响应=客户端。执行(请求);
}捕获(客户端协议例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//将此响应转换为可读字符串
字符串jsonString=null;
试一试{
jsonString=StreamUtils.convertToString(response.getEntity().getContent());
}捕获(非法状态例外e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
//创建一个可以从字符串中使用的JSON对象
JSONObject json=null;
试一试{
json=新的JSONObject(jsonString);
}捕获(JSONException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
试一试{
//JSONArray地震=json.getJSONObject(“数据”).getJSONArray(“项目”);
JSONArray JSONArray=json.getJSONObject(“数据”).getJSONArray(“项目”);
//循环数组
对于(inti=0;i
公共类字符串{ 公共静态void main(字符串[]args){

String str=“45”;
字符串op=addToString(str,“:”);
系统输出打印项次(op);
}
静态字符串addToString(字符串str、字符串ins){

如果(str.length()我已经尝试将其添加到我的代码中,但仍然不影响它]其工作。复制addToString(…)此函数并在需要时调用。因此我使用字符串op显示在文本视图中/那么我如何使用代码中显示的变量字符串?
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);
}