Advertisement
Guest User

Untitled

a guest
May 25th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. public class NetUtils {
  2.     private static Context context = null;
  3.  
  4.     public static void setContext(Context context)
  5.     {
  6.         NetUtils.context = context;
  7.     }
  8.  
  9.  
  10.     public String get(String url, int cache)  {
  11.  
  12.         String result = null;
  13.         Log.d("NetUtils get",":"+url);
  14.         String md5 = Md5Util.md5(url);
  15.         File casheDir = AndroidApplication.cacheDir;
  16.        
  17.         File f = null;
  18.         if (casheDir!=null && cache>0) {
  19.             Log.d("cashe", casheDir.getAbsolutePath());
  20.             f = new File(casheDir, md5);
  21.             final long time = new Date().getTime() / 1000;
  22.             if (f.exists()) {
  23.                 if ((f.lastModified()/1000+cache)>time) {
  24.                     result = readFile(f.getAbsolutePath());
  25.                     if (result.equals("")) {
  26.                         f=null;
  27.                     }
  28.                     else {
  29.                         return result;
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.  
  35.         HttpGet getRequest = new HttpGet(url);     
  36.  
  37.         try {
  38.             HttpResponse getResponse = AndroidApplication.getClient().execute(getRequest);
  39.             final int statusCode = getResponse.getStatusLine().getStatusCode();
  40.             if (statusCode != HttpStatus.SC_OK) {
  41.                 result = "Error:"+statusCode;
  42.             }
  43.  
  44.             HttpEntity getResponseEntity = getResponse.getEntity();
  45.  
  46.             if (getResponseEntity != null && statusCode == HttpStatus.SC_OK) {
  47.                 //result = EntityUtils.toString(getResponseEntity);
  48.  
  49.                     InputStream is = getResponseEntity.getContent();
  50.                     result = convertStreamToString(is,f);    
  51.                     getResponseEntity = null;
  52.                
  53.  
  54.                 if (f!=null) { 
  55.                     BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
  56.                     os.write(result.getBytes());
  57.                     os.flush();
  58.                     os.close();
  59.                 }
  60.             }
  61.         }
  62.         catch (IOException e) {
  63.             getRequest.abort();
  64.             if (f!=null) {
  65.                 if (f.exists()) {
  66.                     Log.d("NetUtils IOException", f.getAbsolutePath());
  67.                     return readFile(f.getAbsolutePath());
  68.                 }
  69.             }
  70.             result = "Error:"+e.toString();
  71.         }
  72.         //Log.d("NetUtils result:",result.substring(0, Math.min(result.length(), 150)));
  73.         return result;
  74.     }
  75.     private String readFile(String filePath) {
  76.         StringBuffer fileData = new StringBuffer(0);
  77.         try {
  78.        
  79.             BufferedReader reader = new BufferedReader(new FileReader(filePath));
  80.             char[] buf = new char[16192];
  81.             int numRead=0;
  82.             while((numRead=reader.read(buf)) != -1){
  83.                 fileData.append(buf, 0, numRead);
  84.             }
  85.             Log.d("FILEData", fileData.toString());
  86.             reader.close();
  87.         } catch (IOException e) {
  88.             e.printStackTrace();
  89.         }
  90.         return fileData.toString();
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement