Advertisement
GeeckoDev

Untitled

Dec 3rd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package org.geeckodev.formadroid.dao;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.HttpStatus;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.impl.client.DefaultHttpClient;
  10. import org.apache.http.util.EntityUtils;
  11. import org.geeckodev.formadroid.model.Lesson;
  12. import org.geeckodev.formadroid.model.Week;
  13.  
  14. import android.util.Log;
  15.  
  16. public class DAO {
  17.     private final String url = "http://vpnetudiant.fr/formafetch/";
  18.     private DefaultHttpClient client;
  19.    
  20.     public DAO() {
  21.         this.client = new DefaultHttpClient();
  22.     }
  23.    
  24.     private String retrieve(String url) {
  25.         HttpGet getRequest = new HttpGet(url);
  26.    
  27.         try {
  28.             HttpResponse getResponse = client.execute(getRequest);
  29.             final int statusCode = getResponse.getStatusLine().getStatusCode();
  30.            
  31.             if (statusCode != HttpStatus.SC_OK) {
  32.                 Log.w(getClass().getSimpleName(), "Error " + statusCode + " for URL " + url);
  33.                 return null;
  34.             }
  35.            
  36.             HttpEntity getResponseEntity = getResponse.getEntity();
  37.            
  38.             if (getResponseEntity != null) {
  39.                 return EntityUtils.toString(getResponseEntity);
  40.             }
  41.         } catch (IOException e) {
  42.             getRequest.abort();
  43.             Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
  44.         }
  45.        
  46.         return null;
  47.     }
  48.    
  49.     private void build(String data, Week week) {
  50.         int day = 0;
  51.        
  52.         week.clear();
  53.        
  54.         for (String i : data.split("\n")) {
  55.             if (i.startsWith(">")) {
  56.                 day++;
  57.             }
  58.            
  59.             String begin = i.substring(0, 6);
  60.             String end = i.substring(7, 13);
  61.             String name = i.substring(14, i.length());
  62.            
  63.             week.getDay(day).addLesson(new Lesson(begin, end, name));
  64.         }
  65.     }
  66.    
  67.     public void find(String group, Week week, Week nextWeek) {
  68.         build(retrieve(this.url + group), week);
  69.         //build(retrieve(this.url + group + "_next"), nextWeek);
  70.     }
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement