Friday, 22 April 2016

Gson Json to Arraylist

  • Copy below mentioned code and paste in GSONUTILS file
    public static <T> ArrayList<T> fromJsontoArrayList(
    String string, Class<T> model)
    {
    
     Gson gson = new GsonBuilder().create();
     T gfromat = null;
     ArrayList<T> localArrayList = new ArrayList<T>();
     try
     {
    
     JSONArray jsonInner = new JSONArray(string);
     int i = 0;
     while (i < jsonInner.length())
     {
     gfromat = gson.fromJson(jsonInner.get(i).toString(), model);
     localArrayList.add(gfromat);
     i++;
    
     }
    
     }
     catch (Exception e)
     {
      Log.e("GsonUtils","Amit exception["+e.getMessage()+"]");
     }
    
     return localArrayList;
    }
  • call this method like this fromJsontoArrayList("jsonstring",beanclass.class)

Friday, 11 March 2016

Convert file size to readable format

[KEY : convert file size, readable file size, convert in]

Convert file size to readable format

  1. Copy below method
    public static String convertSize(long size)
    {
    
    String sizeIn[] = new String[] { "KB", "MB", "GB", "TB" };
    String ret = "";
    if (size >= 1024)
    {
     int devider = 1024;
     int counter = 0;
     while (size / devider > 1024)
     {
      counter++;
      devider *= 1024;
      
     }
     ret = String.format("%.2f", (double) size / devider) + " " + sizeIn[counter];
     
     return ret;
    } else
    {
     ret = size + " bytes";
    }
    return ret;
    }
    
  2. Get file length and call above method
    convertSize(new File("file.txt").length())